STLにForward_Listを実装するC++プログラム
STLのフォワードリストは、単一リンクリストを実装します。リストはforward_listによって異なり、リストは次の要素と前の要素の両方を追跡します。
転送リストは次の要素のみの場所を追跡するため、各要素を保存するために必要なストレージスペースが増加します。 forward_listの欠点は、個々の要素に直接アクセスできず、逆方向に繰り返すことができないことです。
機能と説明:
From main(), we have called following functions: fl.resize() = Returns the resize of forward_list. fl.push_front() = It is used to push elements into a foward_list from the front. fl.remove() = Deletes elements from forward_list. fl.unique() = Deletes duplicate elements from forward_list. fl.reverse() = Reverses the forward_list. fl.front() = Returns the front elements of forward_list
サンプルコード
#include<iostream> #include <forward_list> #include <string> #include <cstdlib> using namespace std; int main() { forward_list<int> fl; forward_list<int>::iterator it; int c, n; while (1) { cout<<"1.Insert Element at the Front"<<endl; cout<<"2.Delete Element at the Front"<<endl; cout<<"3.Front Element of Forward List"<<endl; cout<<"4.Resize Forward List"<<endl; cout<<"5.Remove Elements with Specific Values"<<endl; cout<<"6.Remove Duplicate Values"<<endl; cout<<"7.Reverse the order of elements"<<endl; cout<<"8.Display Forward List"<<endl; cout<<"9.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; switch(c) { case 1: cout<<"Enter value to be inserted at the front: "; cin>>n; fl.push_front(n); break; case 2: n = fl.front(); fl.pop_front(); cout<<"Element "<<n<<" deleted"<<endl; break; case 3: cout<<"Front Element of the Forward List: "; cout<<fl.front()<<endl; break; case 4: cout<<"Enter new size of Forward List: "; cin>>n; if (n <= fl.max_size()) fl.resize(n); else fl.resize(n, 0); break; case 5: cout<<"Enter element to be deleted: "; cin>>n; fl.remove(n); break; case 6: fl.unique(); cout<<"Duplicate Items Deleted"<<endl; break; case 7: fl.reverse(); cout<<"Forward List reversed"<<endl; break; case 8: cout<<"Elements of Forward List: "; for (it = fl.begin(); it != fl.end(); it++) cout<<*it<<" "; cout<<endl; break; case 9: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
出力
1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 2 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 3 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 3 Front Element of the Forward List: 3 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 4 Enter new size of Forward List: 6 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 5 Enter element to be deleted: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 3 2 0 0 0 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 4 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 5 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 8 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 8 5 4 3 2 0 0 0 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 47 Wrong Choice 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 7 Forward List reversed 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 0 0 0 2 3 4 5 8 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 4 Enter new size of Forward List: 4 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 0 0 0 2 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 9 Exit code: 1
-
STLにSet_Intersectionを実装するC++プログラム
2つのセットの共通部分は、両方のセットに共通する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_intersection (set1, set1 + n, set2, s
-
STLにSet_Differenceを実装するC++プログラム
2つのセットの違いは、2番目のセットではなく、最初のセットに存在する要素によってのみ形成されます。関数によってコピーされる要素は、常に同じ順序で最初のセットから取得されます。両方のセットの要素はすでに注文されている必要があります。 一般的な集合演算は-です セットユニオン 交差点を設定 対称集合の差または排他的論理和 差または減算を設定 アルゴリズム Begin Declare set vector v and iterator st. Initialize st = set_difference (set1, set1 + n,