STL總結

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()
表明找到了element

set

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

auto

auto c 自動推斷c的類型字符串

相關文章
相關標籤/搜索