STL map 常用用法詳解

2021-08-19 11:54:50 字數 838 閱讀 6527

stl 通用函式總結

1.標頭檔案:

#include
2.定義:建立key - value的對應

map

mapstudent; //定義乙個用int作為索引,並擁有相關聯的指向string

3.常用操作:

3.1在map中插入元素

mapstudent.insert(pair(1, 「one」));//用insert方法插入pair物件

mapstudet.insert(map

::value_type (1, 「one」));//用insert方法插入value_type物件

mapstudent[1]:」one」;//用陣列方式

mapstudent[2]=」two」;

3.2.從map中刪除元素:

iterator erase(iterator it); //通過乙個條目物件刪除

iterator erase(iterator first, iterator last); //刪除乙個範圍

size_type erase(const key& key); //通過關鍵字刪除

3.3.find(key),返回pair型別指標

4.舉例:

#include 

#include

#include

#include

using

namespace

std;

int main()

C STL Set常用用法詳解

是乙個內部自動有序且不含重複元素的容器。要使用set,需要加標頭檔案 include setname 其中typename可以是任何基本型別,例如int double char 結構體等,也可以是stl標準容器,例如vector set queue等 set iterator it 注 set只能通...

C STL string常用用法詳解

為了方便對字串進行操作,對字串常用的需求功能進行了封裝。要是有string,需加標頭檔案 include string str 1 通過下標訪問 2 通過迭代器訪問 需要注意定義 string iterator it include include using namespace std int m...

C STL replace 函式常用用法詳解

replace演算法 replace函式包含於標頭檔案 include中。泛型演算法replace把佇列中與給定值相等的所有值替換為另乙個值,整個佇列都被掃瞄,即此演算法的各個版本都在 線性時間內執行 其複雜度為o n 即replace的執行要遍歷由區間 frist,last 限定的整個佇列,以把o...