序列容器vector(補充)
阿新 • • 發佈:2019-01-07
//#pragma warning(disable:2679) #include<iostream> //#include<cstdlib> #include<vector> #include<iterator> #include<algorithm> #include<string> #include<cctype> #include"windows.h" using namespace std; class student { public: string name; double ID; student(){}; student(const string &str, const double &id) :name(str), ID(id){} virtual ~student(){} bool operator<(const student &stu){ return ID < stu.ID; } friend ostream& operator<<(ostream &os, const student &stu);//必須加上輸出運算子過載 }; ostream& operator<<(ostream &os, const student &stu) { os << "Name: " << stu.name << "\n" << "Student_ID: " << stu.ID << endl; return os; } void init(vector<student>&dup); void show(const student &stu); bool up5(const student &stu); int main() { vector<student>obj; init(obj); vector<student>::iterator iter; for (iter = obj.begin(); iter != obj.end(); iter++) show(*iter); obj.pop_back(); for (int i = 0; i < obj.size(); i++) { student temp; temp = obj.at(i); show(temp); } sort(obj.begin(), obj.end()); for (auto &a : obj) show(a); student stu = student("xiaowang", 10); obj.insert(obj.begin(),3,stu); for_each(obj.begin(), obj.end(), show); obj.erase(obj.begin(), obj.begin() + 2); cout << "**************\n"; ostream_iterator<student, char> out(cout, " "); copy(obj.begin(), obj.end(), out); cout << "**************\n"; vector<student>obj1; copy(obj.begin(), obj.end(), insert_iterator<vector<student>>(obj1, obj1.begin())); cout << "obj1= "; for_each(obj1.begin(), obj1.end(), show); obj1.clear(); cout << "After obj1.clear(), the size of obj1 is " << obj1.size() << endl; obj.clear(); system("pause"); return 0; } void init(vector<student>&dup) { student temp; char choice; cout << "please key in student's infomation.<y/n>\n"; cin.get(choice).get(); choice = toupper(choice); while (choice == 'Y') { cout << "please input student's name: " << endl; getline(cin, temp.name); cout << "please input student's ID: \n"; cin >> temp.ID; while (cin.get() != '\n') continue; dup.push_back(temp); cout << "Do you want to continue? <y/n>\n"; cin.get(choice).get(); choice = toupper(choice); } cout << "OK. Student infomation is done.\n"; } void show(const student &stu) { cout << "Name: " << stu.name << "\n" << "Student_ID: " << stu.ID << endl; } bool up5(const student &stu) { return stu.ID > 5; }
程式執行結果如下
please key in student's infomation.<y/n> y please input student's name: K please input student's ID: 8 Do you want to continue? <y/n> y please input student's name: M please input student's ID: 2 Do you want to continue? <y/n> y please input student's name: X please input student's ID: 12 Do you want to continue? <y/n> n OK. Student infomation is done. Name: K Student_ID: 8 Name: M Student_ID: 2 Name: X Student_ID: 12 Name: K Student_ID: 8 Name: M Student_ID: 2 Name: M Student_ID: 2 Name: K Student_ID: 8 Name: xiaowang Student_ID: 10 Name: xiaowang Student_ID: 10 Name: xiaowang Student_ID: 10 Name: M Student_ID: 2 Name: K Student_ID: 8 ************** Name: xiaowang Student_ID: 10 Name: M Student_ID: 2 Name: K Student_ID: 8 ************** obj1= Name: xiaowang Student_ID: 10 Name: M Student_ID: 2 Name: K Student_ID: 8 After obj1.clear(), the size of obj1 is 0 請按任意鍵繼續. . .
要想利用
ostream_iterator<student, char> out(cout, " ");
copy(obj.begin(), obj.end(), out);
來遍歷容器中的類物件元素,必須在類中對於輸出運算子<<進行過載,即在類中宣告友元函式
friend ostream& operator<<(ostream &os, const student &stu);