序列容器vector(补充)

//#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);

猜你喜欢

转载自blog.csdn.net/weixin_43871369/article/details/85462444