1.输出迭代器
1.节点,重载
struct student { string name; int age; }; ostream& operator<<(ostream& out,student stu) { out << stu.age << stu.name ; return out; }
2.main
int main() { //输入流迭代器 int array[6] = { 1,2,3,4,5,6 }; ostream_iterator<int> OIter(cout,"\t"); copy(array, array + 6, OIter); cout << endl; //2. list<student> mylist; student stu[3] = { "女孩",14,"女生",16,"女神",18 }; mylist.assign(stu,stu+3); copy(mylist.begin(), mylist.end(), ostream_iterator<student>(cout, "\n")); return 0; }
结果:
2.输入流迭代器(固定搭配)
1.main
int main() { istream_iterator<int> istreamObject(cin); istream_iterator<int> Error; vector<int> myVector; while (istreamObject != Error) { myVector.push_back(*istreamObject); istreamObject++; } copy(myVector.begin(), myVector.end(),ostream_iterator<int>(cout, "\t")); cout << endl; return 0; }
结果: