dapaogege

导航

 

函数for_each(迭代器1,迭代器2,函数操作)
如for_each(v.begin(),v.end(),myprint)

//vector存放自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_Name = name; this是c++的指针,用来指向当前对象本身
this->m_Age = age;

} 
string m_Name;
int m_Age;

};

void test02() {
vector<Person> v;
Person p1("aaa", 110);
Person p2("baa", 120);
Person p3("caa", 130);
Person p4("daa", 140); 自定义类型位置的队列
Person p5("eaa", 150);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
for (vector<Person
>::iterator be = v.begin(); be != v.end(); be++) {
cout << (be)->m_Age << (be)->m_Name << endl;
cout << ((be)).m_Age << ((be)).m_Name << endl;

}

}

void my_print(vector& va) {
for (vector::iterator p = va.begin(); p != va.end(); p++) { 容器嵌套容器
}
}
void test01() {
vector<vector> v;

vector<int> v1;
vector<int> v2;
vector<int> v3;
vector<int> v4;
for (int i = 0; i < 4; i++) {
	v1.push_back(i + 1);
	v2.push_back(i + 2);
	v3.push_back(i + 3);
	v4.push_back(i + 4);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
	for_each(v.begin(), v.end(), my_print);               其中for_each(),三个参数分别是,从给定迭代器开始,到指定迭代器结束,第三个是对每个类型进行的处理
	cout << *p <<" ";

}

posted on 2025-11-29 15:31  dapaogege  阅读(0)  评论(0)    收藏  举报