容器顾名思义,就是装物体的工具,在计算机程序中,就是放数据和缓存数据的工具,容器分为关联容器和非关联容器,下面对几种常见的容器进行举例。
1.vector
#include <iostream>
// 引入头文件
#include <vector>
using namespace std;
int main()
{
// 创建一个向量对象,长度为5
vector<int> vec(5);
cout <<"size:"<<vec.size() << endl;
// 取出元素值
cout << vec.at(0) << " " << vec[1] << endl;
// 向后添加元素
vec.push_back(12);
cout << vec.size() << endl; // 此时长度为6 动态扩充
// 删除最后一个元素
vec.pop_back();
// 在第二个位置插入元素222
vec.insert(vec.begin()+1,222);
// 在倒数第二个位置插入元素4
vec.insert(vec.end()-1,4);
// 删除倒数第一个元素
vec.erase(vec.end()-1);
cout << "context:" ;
for(int i=0;i<vec.size();i++)
{
cout << vec.at(i) << " ";
}
cout << endl;
// 清空元素
vec.clear();
// 判断是否为空
cout <<"result:"<< vec.empty() << endl;
}
运行结果
size:5
0 0
6
context:0 222 0 0 0 4
result:1
2.set
3.list
#include <iostream>
// 引入头文件
#include <list>
using namespace std;
void printList(const list<string> &L) {
cout << "context:";
for (list<string>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main()
{
// 创建一个长度为4的列表对象,每个元素的初始值为"hello"
list<string> lis1(4,"hello");
cout <<"size:" << lis1.empty() << endl;
// 向后追加
lis1.push_back("back");
// 先前追加
lis1.push_front("qian");
cout <<"size:"<< lis1.size() << endl; // 6
printList(lis1);
// 在第一个位置插入元素
lis1.insert(lis1.begin(),"first");
// 在最后一个位置插入元素
lis1.insert(lis1.end(),"finish");
printList(lis1);
// 删除第一个元素
lis1.pop_front();
// 删除最后一个元素
lis1.pop_back();
printList(lis1);
// 第二个位置插入元素
// 【注意】list的迭代器指针不支持+,可以使用自增和自减
lis1.insert(++lis1.begin(),"222222");
// 在倒数第二个位置插入元素
lis1.insert(--lis1.end(),"f68b7s");
printList(lis1);
// 在第5个位置插入一个元素
// 1. 获得起始位置的迭代器指针
// const_iterator:只读
// iterator 读写
list<string>::const_iterator iter = lis1.begin();
// 2. 把迭代器指针向后移动4位
advance(iter,4);
// 3. 使用*取内容,即元素本身
cout << "list[4] = " << *iter << endl;
// 排序
lis1.sort();
// 不支持for循环遍历,但是支持for-each
for(string i:lis1)
{
cout << i << " ";
}
cout << endl;
lis1.clear(); // 清空
cout <<"size:"<< lis1.size() << endl;
}
运行结果
size:0
size:6
context:qian hello hello hello hello back
context:first qian hello hello hello hello back finish
context:qian hello hello hello hello back
context:qian 222222 hello hello hello hello f68b7s back
list[4] = hello
222222 back f68b7s hello hello hello hello qian
size:0
4.map
#include <iostream>
// 引入头文件
#include <map>
using namespace std;
void printMap(const map<int,string> &L) {
cout << "context:";
for (map<int,string>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << it->first << ":" <<it->second<<" ";
}
cout << endl;
}
int main()
{
// 创建一个元素为空的map对象
map<int,string> person;
// 插入元素
person[0] = "zhangsan";
person[11] = "lishi";
person[222] = "wangwu";
person[3333] = "zhaoliu"; // 如果这个键值对已经存在,则表示修改
person.insert(pair<int,string>(44,"lili"));
person.insert(pair<int,string>(55,"tiantian"));
printMap(person);
// 判断一个键是否存在
if(person.find(11) != person.end())
{
// 取出键对应的值
cout <<"ID:11 name:"<< person.find(11)->second << endl;
}else
{
cout << "没有此人" << endl;
}
// 删除键值对
bool result = person.erase(222);
cout << "size:"<<person.size() << endl;
if(result)
cout << "删除人员成功!" << endl;
else
cout << "删除人员失败!" << endl;
printMap(person);
person.clear(); // 清空
cout << person.empty() << endl;
return 0;
}
运行结果
context:0:zhangsan 11:lishi 44:lili 55:tiantian 222:wangwu 3333:zhaoliu
ID:11 name:lishi
size:5
删除人员成功!
context:0:zhangsan 11:lishi 44:lili 55:tiantian 3333:zhaoliu
1