#include <iostream>
#include <string>
#include <algorithm> // 使用算法 需要引入的头文件
#include <vector>
using namespace std;
void callback(int i) {
cout << i << endl;
}
void test1() {
// 操作内置类型
vector<int> v;
for (int i = 0; i < 5; ++i) {
v.push_back(i);
}
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
while (itBegin!=itEnd)
{
cout << *itBegin << endl;
++itBegin;
}
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << endl;
}
for_each(v.begin(), v.end(), callback);
}
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test2() {
//操作自定义类型
Person p1("XiaoWang", 18);
Person p2("XiaoMing", 28);
Person p3("LaoWang", 48);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it) {
cout << (*it).m_name << (*it).m_age <<endl;
}
}
void test3() {
// 操作 自定义类型的指针类型
Person p1("XiaoWang", 18);
Person p2("XiaoMing", 28);
Person p3("LaoWang", 48);
vector<Person*> v;
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
for (vector<Person*>::iterator it = v.begin(); it != v.end(); ++it) {
cout << (*it)->m_name << (*it)->m_age << endl;
}
}
void test4() {
// 操作嵌套类型
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 5; i++) {
v1.push_back(i); // 0 1 2 3 4
v2.push_back(i + 10); // 10 11 12 13 14
v3.push_back(i + 100); // 100 101 102 103 104
}
vector< vector<int> > vv;
vv.push_back(v1);
vv.push_back(v2);
vv.push_back(v3);
for (vector<vector<int>>::iterator it = vv.begin(); it!=vv.end(); ++it) {
for (vector<int>::iterator subit = (*it).begin(); subit != (*it).end(); ++subit) {
cout << (*subit) << endl;
}
}
}
int main()
{
//test1();
//test2();
//test3();
test4();
return 0;
}
C ++ STL three components (container | algorithm | iterators) initially use 01
Guess you like
Origin blog.csdn.net/weixin_43903378/article/details/104076601
Recommended
Ranking