C 語言 STL容器的常規操作(一)

2021-10-08 17:00:31 字數 2545 閱讀 6909

想必各位程式設計師大大多多少少都知道一些stl的知識吧

可是那一串串的點加上一堆英文單詞真的好難記啊……

今天,我就帶大家來看一看,stl中一些模板的常規操作吧!

我們介紹的第乙個stl工具就是map了,翻譯成英語叫做【地圖】,也有【對映】的意思,在這裡,map的意思叫做對映

文 | 戚洪昊

** | 戚洪昊

變數.insert()

這個語句的意思是插入,但是在map當中,不能有重複的,而且會自動從大到小排序,所以,不能再map語句中使用sort相關函式

示例程式:

#include

#include

#include

using

namespace std;

intmain()

程式輸出:hello_world!

變數.clear()

這個語句的意思是清空變數中的全部內容,我們來看一看運用:

給上乙個**加入m.clear以後在輸出,會怎麼樣?

#include

#include

#include

using

namespace std;

intmain()

程式輸出:正常輸出:hello_world!

加入清空**:

變數.erase

這個語句的意思是清空變數中的指定內容,我們來看一看運用:

給上乙個**去掉h以後在輸出,會怎麼樣?

#include

#include

#include

using

namespace std;

intmain()

程式輸出:正常輸出:hello_world!

清空後:ello_world!

變數.push_back()

這個東東是可以在vector變數後面新增資料的語句,下面來看**:

#include

#include

using

namespace std;

intmain()

for(

int j=

0;j<

10;j++

)}

程式輸出:0123456789

vector排序

#include

"stdlib.h"

#include

#include

using

namespace std;

//公升序比較函式

intcompare1

(const

int&a,

const

int&b)

//降序比較函式

intcompare2

(const

int&a,

const

int&b)

intmain()

//遍歷輸出

printf

("排序前資料:");

for(vector<

int>

::iterator it = v.

begin()

; it != v.

end(

); it++

)//公升序排序

sort

(v.begin()

, v.

end(

), compare1)

;//遍歷輸出

printf

("\n公升序後資料:");

for(vector<

int>

::iterator it = v.

begin()

; it != v.

end(

); it++

)//降序排序

sort

(v.begin()

, v.

end(

), greater<

int>()

);//遍歷輸出

printf

("\n降序後資料:");

for(vector<

int>

::iterator it = v.

begin()

; it != v.

end(

); it++

)getchar()

;return1;

}

創作公告:今天先介紹vectormap兩個,後續內容盡請期待!

STL 容器的常用操作

基礎資料結構 deque 佇列 雙向表 list 雙向鍊錶 vector 單向鍊錶 include include include include include include include include includeusing namespace std int main01 a.pop ...

C 容器(一)C 標準模板庫(STL)和容器

c 標準模板庫其實屬於c 標準庫的一部分,c 標準模板庫主要是定義了標準模板的定義與宣告,而這些模板主要都是類模板,我們可以呼叫這些模板來定義乙個具體的類 使用stl不需要自己手動建立乙個函式模板或者是類模板,這些模板都定義在標準模板庫中,我們只需要學會怎麼使用這些類模板來定義乙個具體的類,然後能夠...

STL系列 關聯容器的操作

在c 中定義了幾種型別用來表示容器關鍵字和值的型別。對於set,由於儲存的值就是關鍵字,所以,key type和value type是一樣的。set的迭代器是const的!對於set,find呼叫返回的是乙個迭代器,如果我們所給定的關鍵字在set中的時候,迭代器就指向該關鍵字,否則,find返回尾後...