map常見操作

2021-09-26 10:38:08 字數 3750 閱讀 2916

1,map簡介

map是stl的乙個關聯容器,它提供一對一的hash。

map以模板(泛型)方式實現,可以儲存任意型別的資料,包括使用者自定義的資料型別。map主要用於資料一對一對映(one-to-one)的情況,map內部的實現自建一顆紅黑樹,這顆樹具有對資料自動排序的功能。在map內部所有的資料都是有序的,後邊我們會見識到有序的好處。比如乙個班級中,每個學生的學號跟他的姓名就存在著一對一對映的關係。

2,map的功能

自動建立key - value的對應。key 和 value可以是任意你需要的型別。

3,使用map

使用map得包含map類所在的標頭檔案

#include //注意,stl標頭檔案沒有副檔名.h

map物件是模板類,需要關鍵字和儲存物件兩個模板引數:

std:map<int, string> personnel;

這樣就定義了乙個用int作為索引,並擁有相關聯的指向string的指標.

為了使用方便,可以對模板類進行一下型別定義,

typedefmap<int,cstring> udt_map_int_cstring;

udt_map_int_cstring enummap;

4,map的建構函式

map共提供了6個建構函式,這塊涉及到記憶體分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裡要說下的就是,我們通常用如下方法構造乙個map:

mapmapstudent;

5,插入元素

// 定義乙個map物件

mapmapstudent;

// 第一種 用insert函式插入pair

mapstudent.insert(pair(000, "student_zero"));

// 第二種 用insert函式插入value_type資料

mapstudent.insert(map::value_type(001, "student_one"));

// 第三種 用"array"方式插入

mapstudent[123] = "student_first";

mapstudent[456] = "student_second";

以上三種用法,雖然都可以實現資料的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函式插入資料,在資料的 插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是不能在插入資料的,但是用陣列方式就不同了,它可以覆蓋以前該關鍵字對 應的值,用程式說明如下:

mapstudent.insert(map::value_type (001, "student_one"));

mapstudent.insert(map::value_type (001, "student_two"));

上面這兩條語句執行後,map中001這個關鍵字對應的值是「student_one」,第二條語句並沒有生效,那麼這就涉及到我們怎麼知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程式如下

// 構造定義,返回乙個pair物件

pairinsert (const value_type& val);

pair::iterator, bool> insert_pair;

insert_pair = mapstudent.insert(map::value_type (001, "student_one"));

if(!insert_pair.second)

cout << ""error insert new element" << endl;

我們通過pair的第二個變數來知道是否插入成功,它的第乙個變數返回的是乙個map的迭代器,如果插入成功的話insert_pair.second應該是true的,否則為false。

6, 查詢元素

當所查詢的關鍵key出現時,它返回資料所在物件的位置,如果沒有,返回iter與end函式的值相同。

// find 返回迭代器指向當前查詢元素的位置否則返回map::end()位置

iter = mapstudent.find("123");

if(iter != mapstudent.end())

cout<7, 刪除與清空元素

//迭代器刪除

iter = mapstudent.find("123");

mapstudent.erase(iter);

//用關鍵字刪除

int n = mapstudent.erase("123"); //如果刪除了會返回1,否則返回0

//用迭代器範圍刪除 : 把整個map清空

mapstudent.erase(mapstudent.begin(), mapstudent.end());

//等同於mapstudent.clear()

8,map的大小在往map裡面插入了資料,我們怎麼知道當前已經插入了多少資料呢,可以用size函式,用法如下:

int nsize = mapstudent.size();
9,map的基本操作函式:c++ maps是一種關聯式容器,包含「關鍵字/值」對

begin() 返回指向map頭部的迭代器

clear() 刪除所有元素

count() 返回指定元素出現的次數

empty() 如果map為空則返回true

end() 返回指向map末尾的迭代器

equal_range() 返回特殊條目的迭代器對

erase() 刪除乙個元素

find() 查詢乙個元素

get_allocator() 返回map的配置器

insert() 插入元素

key_comp() 返回比較元素key的函式

lower_bound() 返回鍵值》=給定元素的第乙個位置

max_size() 返回可以容納的最大元素個數

rbegin() 返回乙個指向map尾部的逆向迭代器

rend() 返回乙個指向map頭部的逆向迭代器

size() 返回map中元素的個數

swap() 交換兩個map

upper_bound() 返回鍵值》給定元素的第乙個位置

value_comp() 返回比較元素value的函式

map下標操作

強烈推薦人工智慧學習 之前提到過有關map下標操作,但是今天這個更複雜一點了,寫下來學習一下。struct node int main map iterator it it maptest.find 2 find函式返回乙個迭代器 if maptest.end maptest.find 1 cout...

Map常見的遍歷方式

存放測試資料 mapmap new hashmap map.put 1 value1 map.put 2 value2 map.put 3 value3 第一種 遍歷鍵的set集合,得到每乙個鍵。set strings map.keyset for string string strings 第二種...

map的常見用法詳解

map可以將任何基本型別對映到任何基本型別 包括stl容器 標頭檔案 include map mp 第乙個是鍵的型別,第二個是值的型別 如果是字串對映整型 那麼必須使用string而不是char通過下標訪問 map char int mp mp c 3 cout mp c 通過迭代器訪問 map可以...