stack
Stack(堆栈)是一个容器类的改变,为程序提供了堆栈的全部功能,实现了一个先进后出的数据类型。
- empty() 堆栈为空返回真;
- pop()移除栈顶元素;
- push()在栈顶增加元素;
- size()返回栈中元素;
- top()返回栈顶元素
map
Map是STL的一個容器,它提供一對一的hash。第一个(key),只能在map中出現一次,第二个为值(value)。map内部自建一颗红黑树,可以自动排序。
变量声明
map<string, string> mapStudent;
插入
mapStudent.insert(pair<string, string>("r001", "student_zero"));
mapStudent.insert(make_pair("r002", "student_zero"));
mapStudent.insert(map<string, string>::value_type ("r003", "student_one"));
//array方式插入,该方式可以覆盖map中已有的key对应的value
mapStudent["r000"] = "student_first";
【STL map】map用法、插入方法、pair和make_pair的区别
查找
iter = mapStudent.find("r123");
遍历
for (iter = student.begin(); iter != student.end(); iter++)
{
cout <<iter->first << " " << iter->second << endl;
}
删除
//迭代器刪除
iter = mapStudent.find("r123");
mapStudent.erase(iter);
//用关键字刪除
int n = mapStudent.erase("r123");//如果刪除了会返回1,否则返回0
//用迭代器范围刪除 : 把整个map清空
mapStudent.erase(mapStudent.begin(), mapStudent.end());
//等同于mapStudent.clear()
string
字符串不用多做介绍,这里想记录一下一种for range的遍历方式
#include<iostream>
using namespace std;
int main()
{
string s("hello world");
for(auto c:s)
c='t';
cout<<s<<endl;//结果为hello world
for(auto &c:s)
c='t';
cout<<s<<endl; //结果为ttttttttttt