技巧 C C 常用庫函式

2021-10-01 22:22:41 字數 2555 閱讀 2263

一、map相關

1、map定義時新增引數 less<>/greater<>可以設定map內排序為公升降序;

2、map.find(val)返回迭代器,失敗是迭代器指標指向map.end()

3、map.erase(it), map.erase(it1, it2),map.erase(key)

二、algorithm庫

1、max(a, b), min(a, b), abs(val)

2、swap(a, b)

3、reverse(it1, it2)逆轉序列

4、sort(it1,it2, cmp),cmp可在引數中定義: ( type a, type b )

5、stable_sort(it1, it2, cmp)穩定排序

6、fill(it1, it2, val)

7、max_element (it1, it2, cmp);返回指標

三、vector相關

1、vector.push_back()尾部插入

2、vector.pop_back()

3、vector.earase(it) vector.erase(it1, it2)

4、vector.insert(it, elem)

5、vector.back()

6、重新初始化盡量使用fill(),避免使用resize()

四、set(有序自動去重集合)相關

1、僅支援set.insert()

2、set.find(val),失配返回set.end()

3、set.erase(val) set.erase(it), set.erase(it1, it2)

4、儲存結構體型別需要對運算子』<'過載。

例:對結構體node的'<'過載,實現按node.num_a公升序排序;

typedef

struct node};

set> ss;

五、string相關

1、支援+、+=

2、支援比較運算子(字典序)

3、str.erase(it), str.erase(it1, it2), str.erase(pos, len)

4、str.find(substr)返回首次出現位置,失配返回-1

5、str.substr(pos, len)取子串

6、str.insert(pos, string)

6、str.replace(pos, len, str2);將str下標pos起長度為len的字串替換為str2

7、scanf()輸入

string str;

str.

resize

(n);

//必須預申請空間

scanf

("%s"

,&str[0]

);

8、printf()輸出

printf

("%s"

, str.

c_str()

);

9、string轉int:

(1)atoi()

(2)sscanf(char *, 「%d」, &intager);

10、int轉string:

(1)to_string()

(2)sprintf(str, 「%d」, intager);

11、string() 將c字串轉為string

六、queue相關

1、q.push() q.pop(), q.front(),q.rear()

2、s.push() s.pop() s.top()

3、優先順序佇列

#include

#include <>

priority_queue公升序 / greater降序(預設)>

(1)基本資料型別

priority_queue<

int> q;

priority_queue<

int, vector<

int>

, greater<

int>> q

//等價定義

(2)結構體型別過載運算子

struct node

bool

operator

<

(const node a)

const

//過載《運算子};

priority_queue

, greater> q;

//less降序

七、pair相關

1、pair

2、make_pair(val1, val2)

3、pair->first, pair->second

八、vs2008不提供支援的函式/語法

1、unordered_map

2、auto禁用,迭代器使用class::iterator it;

九、結構體初始化賦初值

struct node

};

C C 常用的庫函式

include using namespace std 函式名稱 返回值 isalnum 如果引數是字母數字,即字母或者數字,函式返回true isalpha 如果引數是字母,函式返回true iscntrl 如果引數是控制字元,函式返回true isdigit 如果引數是數字 0 9 函式返回tr...

C C 數學庫函式

所在函式庫為math.h stdlib.h string.h float.h 1.絕對值 int abs int i 返回整型引數i的絕對值 double cabs struct complex znum 返回複數znum的絕對值 double fabs double x 返回雙精度引數x的絕對值 ...

C C 快速排序庫函式

c 自定義比較函式 庫函式比較函式 命名空間std void qsort void base,size t num,size t size,int comparator const void const void include其中const void a表示宣告了乙個常量指標 a,int a指的是...