序列式容器
vector
#include<iostream>
#include<vector>
using namespace std;
//vector可以使用下标访问和迭代器访问
void the_vector()
{
vector<int> the_vector;//创建vector对象
vector<int>::iterator the_iterator;//定义一个迭代器,该迭代器和普通指针作用一样
for (int idx = 0; idx < 10; idx++)//尾插插入元素
the_vector.push_back(idx);
cout << "vector元素个数大小:" << the_vector.size() << endl;
int temp = 0;
the_iterator = the_vector.begin();//该迭代器指向vector的起始位置
cout << "vector头:"<<the_vector.front() << endl;
cout <<"vector尾:"<< the_vector.back() << endl;
cout <<"vector下标访问:" <<the_vector[0] << endl;//下标访问
cout << "vector迭代器访问:"<<*the_iterator << endl;//迭代器访问
while (the_iterator != the_vector.end())//求前10个数的和
{
temp += *the_iterator;
the_iterator++;
}
cout << "vector:"<<temp << endl;
//vector只能从后往前pop
cout << "vector:";
while (!the_vector.empty())
{
cout <<the_vector.back()<<" ";
the_vector.pop_back();
}
cout << endl;
}
list
#include<iostream>
#include<list>
using namespace std;
void the_list()
{
list<int> the_List;//创建一个list对象
list<int>::iterator the_iterator;//定义一个list的迭代器
for (int idx = 0; idx < 10; idx++)
the_List.push_back(idx);
cout << "list元素个数大小:" << the_List.size() << endl;
cout << "list尾节点:" << the_List.back() << endl;//打印尾节点
the_iterator = the_List.begin();
cout << "list_iterator:";
while (the_iterator != the_List.end())
{
cout << *the_iterator << " ";
the_iterator++;
}
int temp = 0;
while (!the_List.empty())//链表不为空
{
temp += the_List.front();
the_List.pop_front();//删除头节点
}
cout << endl;
cout << "list:"<<temp << endl;;
}
stack
#include<iostream>
#include<stack>
using namespace std;
void the_stack()
{
stack<int> the_stack;//创建一个stack对象
for (int idx = 0; idx < 10; idx++)//压栈
the_stack.push(idx);
cout << "stack中元素数目:" << the_stack.size() << endl;//栈中元素的数目
cout << "stack:";
while (!the_stack.empty())//栈不为空
{
cout << the_stack.top() << " ";//打印栈顶元素
the_stack.pop();//出栈
}
cout << endl;
}
queue
#include<iostream>
#include<queue>
using namespace std;
void the_queue()
{
queue<int> the_queue;//创建一个queue对象
for (int idx = 0; idx < 10; idx++)//压入队列
the_queue.push(idx);
cout << "queue元素个数大小:" << the_queue.size() << endl;
cout << "queue队尾元素:"<<the_queue.back() << endl;//打印队尾
cout << "queue:";
while (!the_queue.empty())//判断是否为空
{
cout << the_queue.front() << " ";//打印队头
the_queue.pop();//出队列
}
cout << endl;
}
关联式容器
map
#include<iostream>
#include<map>
using namespace std;
void the_map()
{
map<int,char> the_map;
the_map.insert(pair<int, char>(1, 'a'));//插入元素
the_map.insert(pair<int, char>(5, 'z'));
std::pair<std::map<int, char>::iterator, bool> ret;//第一种插入方法
ret = the_map.insert(std::pair<int, char>(2, 'b'));
std::map<int, char>::iterator the_iteator = the_map.begin();//第二种插入方法
the_map.insert(the_iteator, pair<int, char>(3, 'c'));
the_map.insert(the_iteator,pair<int, char>(4, 'a'));//插入元素
//find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
the_iteator = the_map.find(1);
if (the_iteator != the_map.end())
cout <<"map:"<<the_map.find(1)->second << endl;//根据键值找真值
cout << "the_map:" << the_map.size() << endl;//map中保存的元素个数。
cout << "the_maxmap:" << the_map.max_size() << endl;//map能够保存的最大元素个数。
while (the_iteator != the_map.end())
{
cout << "map:"<<the_iteator->first << "->" << the_iteator->second << endl;//迭代器方式打印map里的值
the_iteator++;
}
}
运行结果