基本容器
顺序容器:
vector 向量
list 链表
deque 双端队列
关联容器:
set 集合
multiset 多重集合
map 映射
multimap 多重映射
hash_set
hash_multiset
hash_map
hash_multimap
vector :
对数组的封装,在尾端增删元素具有较佳的性能,读取速度快
初始化 vector 对象:
vector<T> v1; vector保存类型为T的对象。默认构造函数v1为空
vector<T> v2(v1); v2是v1的一个副本
vector<T> v3(n,i); v3包含n个值为i的元素
vector<T> v4(n); v4包含n个值为0的元素
例如:
vector<int> ivec; 实例化一个ivec
vector<int> ivec1(ivec); 用ivec实例化ivec1
vector 常用函数:
empty() 判断向量是否为空
begin() 返回向量迭代器首元素
end() 返回向量迭代器末元素的下一个元素
clear() 清空向量
front() 第一个数据
back() 最后一个数据
size() 获得向量中数据个数
push_back(elem) 将数据插入向量尾
pop_back() 删除向量尾部数据
迭代器: iterator
例如:
int main(void)
{
vector vec;
vec.push_back("hello");
vector<string>::iterator citer = vec.begin();
for(;citer!=vec.end();citer++)
cout << *citer <<endl;
return 0;
}
list 链表 : 数据插入速度快
map 映射 : key value
map<int,string> m;
pair<int, string> p1(10, "hello");
m.insert(p1);
cout << m[10] << endl;
例程:vector
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <vector>
class Coor
{
// 重载 <<
friend std::ostream &operator<<(std::ostream &out, Coor &coor);
public:
Coor(int x = 0, int y = 0);
private:
int m_iX;
int m_iY;
};
Coor::Coor(int x, int y)
{
m_iX = x;
m_iY = y;
}
// 有元函数不需要加 Coor:: 标识
std::ostream &operator<<(std::ostream &out, Coor &coor)
{
out << coor.m_iX << "," << coor.m_iY;
return out;
}
int main()
{
// 向量中保存5个3
std::vector<int> vec(5, 3);
for (unsigned int i = 0; i < vec.size(); i++)
std::cout << vec[i] << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 通过 push_back 向向量中保存2和4
std::vector<int> vec1;
if (vec1.empty())
std::cout << "vector vec1 is empty." << std::endl;
vec1.push_back(2);
vec1.push_back(5);
vec1.push_back(4);
for (unsigned int i = 0; i < vec1.size(); i++)
std::cout << vec1[i] << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 访问 vec1 的第一个元素
std::cout << vec1.front() << std::endl;
// 访问 vec1 的第二个元素
std::cout << vec1.back() << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 清除最后一个元素
vec1.pop_back();
std::cout << vec1.back() << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 传入自定参数
Coor coor1(7, 8);
Coor coor2(9,10);
std::vector<Coor> vec3;
vec3.push_back(coor1);
vec3.push_back(coor2);
for (unsigned int i = 0; i < vec1.size(); i++)
std::cout << vec3[i] << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
//使用迭代器iterator遍历模板
std::vector<Coor>::iterator iter = vec3.begin(); // 赋初值
for (; iter != vec3.end(); iter++)
std::cout << *iter << std::endl;
system("pause");
return 0;
}
运行结果:
3
3
3
3
3
----------------------------------------------------
vector vec1 is empty.
2
5
4
----------------------------------------------------
2
4
----------------------------------------------------
5
----------------------------------------------------
7,8
9,10
----------------------------------------------------
7,8
9,10
链表:list
链表只能用迭代器iterator遍历list对象中的所有元素。
内置函数empty, push_back, pop_back, size, front, back
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <list>
class Coor
{
// 重载 <<
friend std::ostream &operator<<(std::ostream &out, Coor &coor);
public:
Coor(int x = 0, int y = 0);
private:
int m_iX;
int m_iY;
};
Coor::Coor(int x, int y)
{
m_iX = x;
m_iY = y;
}
// 有元函数不需要加 Coor:: 标识
std::ostream &operator<<(std::ostream &out, Coor &coor)
{
out << coor.m_iX << "," << coor.m_iY;
return out;
}
int main()
{
// 向量中保存5个3
std::list<int> list(5, 3);
for (std::list<int>::iterator iter = list.begin(); iter != list.end(); iter++)
std::cout << *iter << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 通过 push_back 向向量中保存2和4
std::list<int> list1;
if (list1.empty())
std::cout << "list list1 is empty." << std::endl;
list1.push_back(2);
list1.push_back(5);
list1.push_back(4);
for (std::list<int>::iterator iter = list1.begin(); iter != list1.end(); iter++)
std::cout << *iter << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 访问 vec1 的第一个元素
std::cout << list1.front() << std::endl;
// 访问 vec1 的第二个元素
std::cout << list1.back() << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 清除最后一个元素
list1.pop_back();
std::cout << list1.back() << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
// 传入自定参数
Coor coor1(7, 8);
Coor coor2(9,10);
Coor coor3(3, 7);
Coor coor4(2, 6);
std::list<Coor> list3;
list3.push_back(coor1);
list3.push_back(coor2);
std::list<Coor>::iterator iter = list3.begin();
list3.insert(iter, coor3); // 插入在头部
list3.insert(iter, 3, coor4); // 插入3个coor4
iter++;
list3.insert(iter, list.begin(), list.end()); // 插入list
iter = list3.begin();
for (;iter != list3.end(); iter++)
std::cout << *iter << std::endl;
system("pause");
return 0;
}
运行结果:
3
3
3
3
3
----------------------------------------------------
list list1 is empty.
2
5
4
----------------------------------------------------
2
4
----------------------------------------------------
5
----------------------------------------------------
3,7
2,6
2,6
2,6
7,8
3,0
3,0
3,0
3,0
3,0
9,10