最近在練習(xí)C++編程,做了一些??秃土凵厦娴念}目,發(fā)現(xiàn)常用的C++ STL 有以下幾種,對此進(jìn)行簡要總結(jié),以便自己及時(shí)復(fù)習(xí)。
文章大多來自轉(zhuǎn)載,感謝社區(qū)程序員的共享。
C++ 中常用的STL 有:
- algorithm:
#include<algorithm>
algorithm 在STL中的應(yīng)用主要是: sort() swap() reverse() find()
max(), min(), abs()
swap()
reverse() //reverse(vi.begin(), vi.end());
sort() // sort(a, a+4);
find() // find(vi.begin(), vi.end(), 3);
next_permutation() // could get all sequence for several numbers. 000_algorithm.cpp
fill() // fill(a, a+5, 233);
*min_element(num,num+6)
*max_element(num,num+6)
lower_bound和upper_bound()
https://blog.csdn.net/weixin_40349531/article/details/88361095
- math:
#include<cmath>
// #include<math.h>
https://blog.csdn.net/zy2317878/article/details/79414431
https://blog.csdn.net/FX677588/article/details/52962798
- vector:
#include<vector>
push_back();
pop_back();
begin();
end();
insert();
erase();
find();
at();
size();
http://blog.chinaunix.net/uid-26000296-id-3785623.html
http://blog.chinaunix.net/uid-26000296-id-3785610.html
https://www.cnblogs.com/aminxu/p/4686332.html
- string:
#include<string>
data(); // 返回指向自己的第一個(gè)字符的指針
+= // 連接操作符
append();
insert();
find(); // if not include, return iterator::end()
replace();
size();
substr();
swap();
http://blog.chinaunix.net/uid-26000296-id-3781405.html
https://www.cnblogs.com/aminxu/p/4686320.html#at
- set:
#include<set>
// set的特性是,所有元素都會根據(jù)元素的鍵值自動排序.
// set的元素不像map那樣可以同時(shí)擁有實(shí)值(value)和鍵值(key),set元素的鍵值就是實(shí)值,實(shí)值就是鍵值。
// set不允許兩個(gè)元素有相同的鍵值.
https://www.cnblogs.com/omelet/p/6627667.html
- map:
#include<map>
// map的特性是,所有元素都會根據(jù)元素的鍵值自動被排序。
// map的所有元素都是pair,同時(shí)擁有實(shí)值(value)和鍵值(key)。
// pair的第一個(gè)元素會被視為鍵值,第二個(gè)元素會被視為實(shí)值。
// map不允許兩個(gè)元素?fù)碛邢嗤逆I值。
map<string , int> strMap; // constructor
strMap.insert(map<string, int>::value_type("a", 1));
strMap["a"] = 1; // for insert, will override 覆蓋之前的值
size();
https://www.cnblogs.com/omelet/p/6617362.html
https://blog.csdn.net/sevenjoin/article/details/81943864
- others:
7.1 int <--> string
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
// int 轉(zhuǎn) string
stringstream ss;
int n = 123;
string str;
ss<<n;
ss>>str;
cout << str << endl;
// string 轉(zhuǎn) int
str = "456";
n = atoi(str.c_str());
cout << n << endl;
return 0;
}
7.2 int number <--> char
char ch = '2';
int in = ch - '0';
in = in + 1;
char chHigher = in + '0';
7.3 random number:
#include <stdlib.h>
#include <iostream>
#include <time.h>
#define MAX 100
using namespace std;
int main()
{
srand( (unsigned)time(NULL) );
for(int i=0; i<10; i++)
cout<<rand()%MAX<<endl;
return 0;
}