int array[5] = [1,2,3,4,5]
int *p = array // 指向数组首地址 &array[0]
// 1
for(int i = 0; i<5; i++) {
cout << array[i] << endl;
}
// 2
for (int i = 0; i<5; i++) {
cout << *(p++) << endl;
}
// 3
#include<vector>
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
vector<int>::iterator itbegin = v.begin()
vector<int>::iterator itend = v.end()
while(itbegin != itend) {
cout << *itbegin <<endl;
itbegin++;
}
// 4
#include<algorithm>
void myPrint(int v) {
cout << v <<endl;
}
for_each(v.begin(), v.end(), myPrint);
#include<vector>
#include<string>
class Person{
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
}
Person p1("大头儿子", 12);
Person p2("小头爸爸", 13);
Person p3("小头妈妈", 14);
Person p4("奶奶", 15);
vector<Person> v;
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;
}