C 之STL庫簡單介紹

2021-10-02 05:59:18 字數 2382 閱讀 1002

1. sort()     可以自己定義新的結構體陣列排序,sort第三個引數可以設定為根據哪個值排序

int a = ;

int n = sizeof(a) / sizeof(int);

sort(a, a + n);

for (int i = 0; i2. string

string s = "46237879426"; // 排序

sort(s.begin(),s.end());

cout << s;

string s = "46237879426";

s.erase(s.begin()); //刪除第乙個

cout << s << endl;

s.erase(--s.end()); //刪除最後乙個

cout << s << endl;

string s = "46237879426";   

s = s.substr(2, 3); //從第二個位置開始,取三個

cout << s << endl;

string s = "46237879426";

cout << s.length() << endl; //長度

3. vector

vectorv;  //空的  初始為0

// vectorv(4,6); //初始化4個6

v.push_back(1);

v.push_back(2);

v.push_back(3);

v.push_back(4);

for (vector::iterator it = v.begin(); it != v.end(); it++)

vectorv = ;

sort(v.begin(), v.end()); //排序,可以加第三個引數即順序和逆序

for(auto x : v) //此處在vs2010中會報錯,使用vs2015沒問題

cout << x;

4. stack()

stacks;  //push,pop,size,empty

s.push(2);

s.push(3);

cout << s.size() << endl;

cout << s.top() << endl;

s.pop();

cout << s.top() << endl;

cout << s.empty() << endl;

s.pop();

cout << s.empty() << endl; //空值返回為1

5.  queue

queueq;

q.push(5);

q.push(6);

cout <6. map

mapm; //有序

m[6]=3;

m[5]=6;

m[4]=9;

for (auto it=m.begin();it!=m.end();it++)

unordered_mapm;   //有序

m[6]=3;

m[5]=6;

m[4]=9;

for (auto it=m.begin();it!=m.end();it++)

7. set

sets;

s.insert(3);

s.insert(1);

s.insert(3);

cout << s.size() << endl;

for (auto it = s.begin();it!=s.end();it++)

unordered_sets;   //無序

s.insert(3);

s.insert(1);

s.insert(3);

cout << s.size() << endl;

for (auto it = s.begin();it!=s.end();it++)

8. deque  雙端佇列

dequed;

d.push_back(1);

d.push_front(2);

d.push_back(3);

d.push_front(4);

for (auto it = d.begin();it!=d.end();it++)

C 的STL之仿函式簡單介紹

include include include 演算法 include 仿函式 using namespace std bool less3 int x int main void 仿函式 繫結函式 第乙個大於30的數字 auto ifind find if myvecyor.begin myvec...

STL簡單函式介紹

unique 函式 以下的內容主要來介紹stl中的常見函式,也就是包含於algorithm標頭檔案內的部分簡單演算法函式。我的自學筆記內容可能並不深入,因此歡迎各位大佬提出建議!在此感謝啦or2 sort 函式是進行陣列排序的函式,其排序時間複雜度平均為n log nn log n nlogn 也就...

C 標準模板庫(STL介紹) string

string str1 ab str2 xy string str str1 str2 比較規則是字典序 方法一 string str abcxyz str2 opq str.insert 3 str2 輸出 abcopqxyz 方法二 string str abcxyz str2 opq str....