遍历int、对象数组

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 v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);

vector::iterator itbegin = v.begin()
vector::iterator itend = v.end()

while(itbegin != itend) {
    cout << *itbegin <

void myPrint(int v) {
    cout << v <
#include
#include

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 v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);

for(vector::iterator it = v.begin(); it != v.end(); it++) {
    cout << (*it).name << it->age << endl;
}




 

你可能感兴趣的:(C++学习,c++)