STL标准模板库
广义分为:容器、算法、迭代器。
大体分为六大组件:容器、算法、迭代器、仿函数、适配器、空间配置器。
容器:vector、list、deque、set、map等
算法:sort find copy for_each等
容器分为序列式容器和关联式容器。
序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置。
关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系。
算法:质变算法和非质变算法
质变算法:运算过程中会更改区间内的元素的内容,例如拷贝、替换、删除等。
非质变算法:运算过程中不会更改区间内的元素内容,例如:查找、计数、遍历、寻找极值等等。
容器:vector 例如:vector<类型>v;
算法:for_each(v.begin(),v.end(),myfunc),myfunc为写的别的函数名
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
void myprint(int p)
{
cout << p << 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 it1 = v.begin();
vector<int>::iterator it2= v.end();
/*while (it1!= it2)
{
cout << *it1 << endl;
it1++ ;
}*/
/*for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}*/
for_each(v.begin(), v.end(), myprint);
}
int main()
{
test01();
system("pause");
return 0;
}
容器嵌套容器
举例如下:
#include<iostream>
using namespace std;
#include <string>
#include <vector>
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test01()
{
Person p1("a", 1);
Person p2("b", 2);
Person p3("c", 3);
Person p4("d", 4);
Person p5("e", 5);
vector<vector<Person>>v;
vector<Person>v1;
vector<Person>v2;
vector<Person>v3;
vector<Person>v4;
vector<Person>v5;
v1.push_back(p1);
v2.push_back(p2);
v3.push_back(p3);
v4.push_back(p4);
v5.push_back(p5);
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
v.push_back(v5);
for (vector<vector<Person>>::iterator vit = v.begin(); vit != v.end(); vit++)
{
for (vector<Person>::iterator it = (*vit).begin(); it != (*vit).end(); it++)
{
cout << it->m_name << " " << it->m_age << endl;
}
}
}
int main()
{
test01();
system("pause");
return 0;
}