关于c++:STL总结

5次阅读

共计 1034 个字符,预计需要花费 3 分钟才能阅读完成。

Vector

  • 数组初始化 int a[5] = {1,2,3,4,5}
  • vector 初始化 vector<int> list = {1,2,3,4,5}
  • vector<int> nums;
  • vector<vector<int>> f1(n, vector<int>(m,0));
  • sort(nums.begin(), nums.end());
  • nums.push_back(x);
  • nums.insert(nums.begin(), x);
  • nums.insert(nums.begin() + n , x);
  • nums.erase(nums.begin());
  • int rows=triangle.size();// 求得行数
  • int col=triangle[0].size();// 求的列数
  • auto x = max_element(a.begin() + i, a.end()); 用的时候 *x
  • 整型转字符串
  • to_string(i)
  • 字符串转整型
  • int a=atoi(s.c_str());
  • int b=stoi(s);
  • vector 中的 find()
  1. vector<int>::iterator result = find(arr2.begin(), arr2.end(), arr1[i]);
  2. if (result == arr2.end() ) // 如果没找见
  • 去重 alls.erase(unique(alls.begin(), alls.end()), alls.end());

String

  • str.insert(str.begin(), ‘a’)
  • 切分字符串:str.substr(str.begin(), str.end())
  • 字符串增加元素:str.push_back(‘a’)
  • 字符串删除开端元素:str.pop_back(‘a’)
  • 删除元素 str.substr(0, str.length() – 1);
  • 删除元素 str.erase(str.end() – 1);

map

  • map<int, int> loc;

获取键、值
for(auto x : f1) x.first,x.second

s.find() 查找一个元素,如果容器中不存在该元素,返回值等于 s.end()
if(numSet.find(findNum)!=numSet.end()
代表找到了

set

  • set.insert();
  • set 遍历
  1. set<int>::iterator it;
  2. for(it=notAppearSet.begin ();it!=notAppearSet.end ();it++) cout << *it;

auto

auto c 主动推断 c 的类型

正文完
 0