序列容器list,queue,vector
阿新 • • 發佈:2019-01-07
#include<iostream> #include<utility> #include<cctype> #include<cstdlib> #include<ctime> #include<string> #include<vector> #include<queue> #include<stack> #include<list> #include<iterator> #include<algorithm> #include"windows.h" using namespace std; typedef vector<pair<string,int>> vec; void init(vec &v); void show(const pair<string, int>&p); enum color{ red, orange, yellow, green, blue,white,black }; char *COLOR[] = { "red", "orange", "yellow", "green", "blue", "white", "black" }; struct object { color c; int size; string type; }; void display(const object &obj); int main() { vec v1; init(v1); vec::reverse_iterator riter; cout << "Original v1:\n"; for (riter = v1.rbegin(); riter != v1.rend(); riter++) show(*riter); vec v2(v1.rbegin(), v1.rbegin() + 1); cout << "Original v2:\n"; for_each(v2.rbegin(), v2.rend(), show); copy(v1.begin(), v1.begin() + 1, back_insert_iterator<vec>(v2)); cout << "copy v1.begin() to v2:\n"; for (auto &x : v2) show(x); int num[] = { 1, 2, 3, 4, 5, 6 }; list<int>li; li.insert(li.begin(), num, num + 6); cout << "Original li:\n"; ostream_iterator<int, char>out(cout, " "); copy(li.begin(), li.end(), out); cout << endl; list<int>li2(4, 2); li.merge(li2);//排序後 cout << "merge(li2):\n"; copy(li.begin(), li.end(), out); cout << endl; li.sort(); li.unique(); cout << "li.unique():\n"; copy(li.begin(), li.end(), out); cout << endl; li.remove(2); cout << "li.remove(2):\n"; copy(li.begin(), li.end(), out); cout << endl; list<int>li3; li3.insert(li3.begin(), num + 2, num + 5); li.splice(li.begin(), li3); cout << "li.splice(li.begin(), li3):\n"; copy(li.begin(), li.end(), out); cout << endl; queue<object>qu; if (qu.empty()) cout << "Now,this queue is empty.\n"; vector<object>obj(3); obj.at(0) = { black, 120, "pig" }; obj[1] = { white, 3, "cat" }; qu.push(obj[0]); qu.push(obj[1]); cout << "Now,this queue's size is:" << qu.size() << endl; cout << "Now,the top element of queue is:" << endl; display(qu.front()); cout << "Now,the bottom element of queue is:" << endl; display(qu.back()); qu.pop(); cout << "After pop(),the top element of queue is:" << endl; display(qu.front()); stack<string>st; for (auto &x : COLOR) st.push(x); cout << "The top element of stack is:" << st.top() << endl; st.pop(); cout << "After pop(), the top element of stack is:" << st.top() << endl; cout << "its size is:" << st.size() << endl; system("pause"); return 0; } void init(vec &v) { srand(time(0)); pair<string, int>temp; cout << "please input your choice to continue<y/n>:"; char choice; cin.get(choice).get(); choice = toupper(choice); while (choice == 'Y') { cout << "please input student's name:"; cin >> temp.first; while (cin.get() != '\n') continue; temp.second = rand() % 20;//string first;int second;temp=make_pair(first,second); v.push_back(temp); cout << "Do you continue<y/n>:"; cin.get(choice).get(); choice = toupper(choice); } cout << "Done!\n"; return ; } void show(const pair<string, int>&p) { cout << "Name: " << p.first << ",ID: " << p.second << endl; } void display(const object &obj) { cout << "Color:" << COLOR[obj.c] << "\nweight:" << obj.size << "kg\nType:" << obj.type << endl; }
程式執行結果如下
please input your choice to continue<y/n>:y please input student's name:xiaoming Do you continue<y/n>:y please input student's name:xiaowang Do you continue<y/n>:n Done! Original v1: Name: xiaowang,ID: 5 Name: xiaoming,ID: 12 Original v2: Name: xiaowang,ID: 5 copy v1.begin() to v2: Name: xiaowang,ID: 5 Name: xiaoming,ID: 12 Original li: 1 2 3 4 5 6 merge(li2): 1 2 2 2 2 2 3 4 5 6 li.unique(): 1 2 3 4 5 6 li.remove(2): 1 3 4 5 6 li.splice(li.begin(), li3): 3 4 5 1 3 4 5 6 Now,this queue is empty. Now,this queue's size is:2 Now,the top element of queue is: Color:black weight:120kg Type:pig Now,the bottom element of queue is: Color:white weight:3kg Type:cat After pop(),the top element of queue is: Color:white weight:3kg Type:cat The top element of stack is:black After pop(), the top element of stack is:white its size is:6 請按任意鍵繼續. . .