C++ STL 学习笔记

1、Map和Multimap

 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理。他们根据key的排序准则将元素排序。multimap允许重复元素,map不允许。

元素要求:

  • key/value必须具有assigned(可赋值)和copyable(可复制的)性质。
  • 对于排序而言,key必须具是comparable(可比较的)。

C++ STL 学习笔记_第1张图片

2、使用append在string后添加n个相同的字符

string a="abcd"

a.append(6, 'e')   //a="abcdeeeeee",在a后面添加6个‘e’

3、使用count统计vector中某个元素的个数

vector tmp={1,2,3,4,5,2,2};
int num=count(tmp.begin(),tmp.end(),2);  //num等于3

int num=count(tmp.begin(),tmp.end(),9);  //num等于0

4、使用find确认vector中是否存在某元素

vector a={1,2,3,4,5,6};
auto val=find(a.begin(),a.end(),27);   //27不存在,val==a.end()

auto val=find(a.begin(),a.end(),5);    //5在容器a中,*val==5

5、使用erase删除指定位置的元素

    X.erase(X.begin()+index)

6、next_permutation 可以获得字符串的下一个全排列

vector ret;
ret.emplace_back(str);
while (next_permutation(str.begin(), str.end()))
{
    ret.emplace_back(str);
}

7、vecotr.data()

  std::vector myvector (5);

  int* p = myvector.data()

 

你可能感兴趣的:(C++)