国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

STL中map,set的使用

2019-11-08 19:48:38
字體:
來源:轉載
供稿:網友

Map: 1.定義: Map是標準關聯式容器之一,一個map是一個鍵值對序列,即(key ,value)對。它提供基于key的快速檢索能力,在一個map中key值是唯一的。

2.Map常用的方法主要有:insert,erase,size,count,begin,end,find,clear,empty。

insert方法:在map中插入一個元素,map中記錄的元素通常為鍵值對,所以,在存儲時會把,鍵和值封裝成pair然后進行插入,例如:mymap.insert(pair < string,string>(str,num));其中str和num為string類型的變量。當然也可以簡單的寫成mymap[str]=num;此處mymap即為map<string,string>類型的變量。因為map在實現過程中對[]進行了重載。 第一種方式若插入的元素的鍵值已經存在于map中,那么就會插入失敗,不會修改元素的鍵值對信息,若鍵值在map中查找不到,那么就會將該新元素加入到map中去。 第二種方式比較直觀,但存在一個性能的問題。插入時,先在mymap中查找主鍵為str的項,沒發現,然后將一個新的對象插入mymap,鍵是str,值是一個空字符串,插入完成后,將字符串賦為num, 該方法會將每個值都賦為缺省值,然后再賦為顯示的值,如果元素是類對象,則開銷比較大。若找到鍵值為str的項,則用num更改原來的num值。

erase方法: erase主要是刪除map中的某個項,需要參數key,例如mymap.erase(name);此句的意思就是刪除key值為str的鍵值對。

size方法: 統計map中鍵值對的個數,mymap.size()返回值即為mymap中鍵值對的個數,若map為空則返回0。

count方法: 統計map中某個鍵值出現的次數,因為map中鍵值唯一,所以此方法可以用來檢測某鍵值是否存在,例如在刪除時可以mymap.count(str),若為0則可以提示用戶此鍵值不存在,若為1則直接刪除。

begin、end方法: begin方法返回map迭代器類型,通過此迭代器與end方法的返回值進行比較就可以很容易的對map進行遍歷。

find方法: 查找某個鍵在map中的位置。

clear方法: 清空map中的所有元素

empty方法: 判斷map是否為空,若為空則返回真若非空則返回假。

用例代碼如下:

#include<iostream>#include<map>#include<string>using namespace std;int main(){ map<string,int> mymap; map<string,int>::iterator it; //插入 mymap.insert ( pair<string,int>("string",1) ); mymap.insert ( pair<string,int>("sort",2) ); mymap.insert ( pair<string,int>("erase",3) ); mymap.insert ( pair<string,int>("begin",4) ); mymap["find"] = 5; mymap.erase("find");//刪除 it = mymap.find("begin"); mymap.erase(it); cout<<"map中鍵隊的個數:"<<mymap.size()<<endl; cout<<"map是否為空?"<<mymap.empty()<<endl; for ( it=mymap.begin() ; it != mymap.end(); it++ ) { cout << (*it).first << " => " << (*it).second << endl; } mymap.clear();//全刪除 cout<<"map是否為空?"<<mymap.empty()<<endl; for ( it=mymap.begin() ; it != mymap.end(); it++ ) { cout << (*it).first << " => " << (*it).second << endl; } return 0;}

結果: 這里寫圖片描述

set的用法: 1.定義: set(集合)是一個容器,它其中所包含的元素的值是唯一的。set支持唯一鍵值,set中的值都是特定的,而且只出現一次。 2.set常用的主要方法有:insert,erase,find,count,size,begin,end等。

1、插入:insert() 1)pair<iterator,bool> insert ( const value_type& x ) 在迭代器中插入一個數據,如果這個數據不存在就直接插入,其返回值為插入后元素的迭代器和true。如果這個元素已經存在,那么返回當前元素以及false. 2)iterator insert ( iterator position, const value_type& x ) 在指定的位置插入指定的數據,position是一個迭代器的位置,x表示的是要插入的數。如果插入成功的話,會返回一個插入新位置的迭代器。 3)template<class InputIterator> void insert(InputIterator first,InputIterator last) 插入一段迭代器區間 **2、刪除(erase) 1)void erase(iterator position)** 刪除一個迭代器位置 2)size_type erase(sonst key_type& x) 刪除成功的話就返回1 3)void erase(iterator first, iterator last) 刪除一段迭代器區間 3、查找:find() iterator find(const key_type& x) const; 若找到返回該迭代器的位置,否則就返回的是最后一個元素后面的位置 4、數量:count() **size_type count(const key_type& x)con**st count是為了統計某個數值出現的次數,在set里面不能重復出現相同的數值,count的返回值只有0和1兩個值,0表示的是不存在,1表示存在。

用例代碼如下:

#include<iostream> using namespace std; #include<set> void fun(set<int> myset) { set<int>::iterator it; for (it=myset.begin(); it!=myset.end(); it++) { cout <<*it<<" "; } cout << endl; } void TestSet() { set<int> t; set<int>::iterator it; pair<set<int>::iterator,bool> ret; //第一種插入方法 cout<<"插入1,2,3,4,5:"<<endl; t.insert(1); t.insert(2); t.insert(3); t.insert(4); t.insert(5); fun(t); cout<<endl; cout<<"再次插入2:"<<endl; ret = t.insert(2); if(ret.second == false) { it=ret.first; } cout<<*it<<endl; //第二種插入 ,在某一位置插入 cout<<"在某一位置插入7,8,9:"<<endl; t.insert (it,7); t.insert (it,8); t.insert (it,9); fun(t); cout<<endl; //第三種插入 ,將某個區間插入 cout<<"將一個數組插入:"<<endl; int a[]= {10,11,12}; t.insert (a,a+3); fun(t); cout<<endl; cout<<"10是否存在:"<<endl; cout<<"10 count:"<<t.count(10)<<endl; cout<<"size:"<<t.size()<<endl; cout<<"刪除1:"<<endl; t.erase (1); fun(t); cout<<endl; cout<<"刪除3:"<<endl; it=t.begin(); it++; t.erase (it); fun(t); cout<<endl; cout<<"刪除11及以后的數:"<<endl; it=t.find (11); t.erase ( it, t.end() ); fun(t); cout<<endl; } int main() { TestSet(); return 0; }

結果如下: 這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拜泉县| 扶绥县| 齐齐哈尔市| 饶平县| 浑源县| 苏尼特左旗| 亚东县| 夏河县| 东乡县| 凤台县| 铁力市| 兴海县| 安阳市| 安顺市| 沿河| 盖州市| 安阳县| 德江县| 浦北县| 余江县| 同心县| 平乡县| 综艺| 崇左市| 淳安县| 盘锦市| 湘潭县| 老河口市| 公安县| 丰台区| 宁都县| 内乡县| 江口县| 公主岭市| 射阳县| 云梦县| 西平县| 黄骅市| 平昌县| 锡林郭勒盟| 峨边|