c++ 常用STL整理

最近在練習(xí)C++編程,做了一些??秃土凵厦娴念}目,發(fā)現(xiàn)常用的C++ STL 有以下幾種,對此進(jìn)行簡要總結(jié),以便自己及時(shí)復(fù)習(xí)。

文章大多來自轉(zhuǎn)載,感謝社區(qū)程序員的共享。

C++ 中常用的STL 有:

  1. 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

  1. math:
    #include<cmath>
    // #include<math.h>

https://blog.csdn.net/zy2317878/article/details/79414431
https://blog.csdn.net/FX677588/article/details/52962798

  1. 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

  1. 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

  1. set:
    #include<set>
// set的特性是,所有元素都會根據(jù)元素的鍵值自動排序.
// set的元素不像map那樣可以同時(shí)擁有實(shí)值(value)和鍵值(key),set元素的鍵值就是實(shí)值,實(shí)值就是鍵值。
// set不允許兩個(gè)元素有相同的鍵值.

https://www.cnblogs.com/omelet/p/6627667.html

  1. 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

  1. 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;
}

https://www.jb51.net/article/124108.htm

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原創(chuàng)鏈接 一、Java面試題java有多重要,對于做android的我們,不需要多說了,let’s go (1)J...
    李福來閱讀 2,443評論 0 5
  • 1.ConstraintLayout:http://www.itdecent.cn/p/17ec9bd6ca8a...
    啊了個(gè)支閱讀 1,560評論 0 0
  • 包含的重點(diǎn)內(nèi)容:JAVA基礎(chǔ)JVM 知識開源框架知識操作系統(tǒng)多線程TCP 與 HTTP架構(gòu)設(shè)計(jì)與分布式算法數(shù)據(jù)庫知...
    消失er閱讀 4,552評論 1 10
  • dom選擇器。 我們知道document表示文檔,我們就是通過document里面的方法選擇標(biāo)簽。 1:docum...
    yy小閱讀 11,528評論 1 2
  • 楚國討伐宋國來救援鄭國。宋襄公預(yù)備迎戰(zhàn),大司馬固,也就是子魚上前進(jìn)諫說道:“上蒼舍棄商朝已經(jīng)很久了,現(xiàn)在您卻要復(fù)興...
    耑意兒閱讀 679評論 0 1

友情鏈接更多精彩內(nèi)容