vector : 类似数组的一种容器
一般使用和几个遍历方式##
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int& a) {
cout << a << endl;
}
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
for_each(v.begin(), v.end(), myPrint);
}
int main() {
test01();
return 0;
}
vector容器中存放自定义类型数据
class Person {
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
};
void test02() {
vector<Person> v;
Person p1("tom1", 10);
Person p2("tom2", 12);
Person p3("tom3", 15);
Person p4("tom4", 18);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout<< it->name << " " << it->age << endl;
}
}
int main() {
test02();
return 0;
}