日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

std::map用法总结

發(fā)布時(shí)間:2023/12/9 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 std::map用法总结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

給出了map的基本用法如插入、查找、刪除、遍歷等等,同時(shí)告訴你如何實(shí)現(xiàn)雙鍵map,包括

(1) 只有兩個(gè)鍵都匹配才命中目標(biāo)
(2) 兩個(gè)鍵中任意一個(gè)匹配就命中目標(biāo)

可以擴(kuò)展到多鍵


(一) 介紹
特點(diǎn):
1.map將Key的object和T的Object綁定到一起,因此是一種Pair Associative Container, 表示其value type為 pair。
2.它同時(shí)也是Unique Associative Container,表示沒(méi)有兩個(gè)元素具有相同的Key。
3.它還是一種Sorted Associative Container,因此第三個(gè)參數(shù)只能是less,greater之類(lèi)的functor, 相比較而言,
hash table是 equal_to, not_equal_to之類(lèi)的functor。(二) 基本用法
通過(guò)以下范例,可以看出map的一些基本用法: 插入、查找、刪除、遍歷等等。
/* 這個(gè)是MS的bug,看著心煩,屏蔽掉警告 */
#if defined (_MSC_VER)
#pragma warning(disable: 4786)
#endif
#include <iostream>
#include <map>
#include <algorithm>
int main(int argc, char *argv[])
{
??? /* define a map */
??? std::map _map;
???
??? /* insert */
??? _map.insert( std::map::value_type(0, 32.8) );
??? _map.insert( std::map::value_type(1, 33.2) );
??? _map.insert( std::map::value_type(2, 35.8) );
??? _map.insert( std::map::value_type(3, 36.4) );
??? _map.insert( std::map::value_type(4, 37.8) );
??? _map.insert( std::map::value_type(5, 35.8) );
???
??? /* 這個(gè)是常用的一種map賦值方法 */
??? _map[7] = 245.3;
???
??? /* find by key */
??? std::map::iterator itr;
??? itr = _map.find(4);
???
??? if( itr != _map.end() )
??? {
??????? std::cout << "Item:" << itr->first << " found, content: " << itr->second << std::endl;
??? }
???
??? std::cout << std::endl;
???
??? /* delete item from map */
??? if( itr != _map.end() )
??? {
??????? _map.erase(itr);
??? }
???
??? /* travel through a map */
??? std::map::iterator itr1 = _map.begin();
??? for( ; itr1 != _map.end(); ++itr1 )
??? {
??????? std::cout << "Item:" << itr1->first << ", content: " << itr1->second << std::endl;
??? }
???
??? std::cout << std::endl;
???
??? /* empty a map */
??? _map.clear();
???
??? return 0;
}
(三) 當(dāng)Key是結(jié)構(gòu)時(shí)該如何定義結(jié)構(gòu)
比如 Key是結(jié)構(gòu)MyStruct類(lèi)型, 此時(shí)map可以定義如下:
std::map > _map;
其中Compare 缺省是std::less,這里可以不寫(xiě),自定義的結(jié)構(gòu)必須實(shí)現(xiàn)Compare指定的比較操作,因此自定義結(jié)構(gòu)
MyStruct必須按照如下寫(xiě)法:struct MyStruct
{
??? int key;
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? return key < rhs.key;
?? }
};
當(dāng)然也可以實(shí)現(xiàn)全局operator <
bool operator < ( const MyStruct lhs, const MyStruct rhs)
{
??? return lhs.key < rhs.key;
}另外,當(dāng)Compare 是std::greater時(shí),需要實(shí)現(xiàn) operator >(四) 如何實(shí)現(xiàn)兩個(gè)Key的map, 只有兩個(gè)Key都匹配才命中目標(biāo)
可以定義結(jié)構(gòu)MyStruct如下:
struct MyStruct
{
??? int key1;
??? double key2
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? /* 兩個(gè)key必須都匹配才命中 */
??????? return ( key1 < rhs.key1 || key2 < rhs.key2 );
?? }
};(五) 如何實(shí)現(xiàn)兩個(gè)Key的map, 兩個(gè)Key中任意一個(gè)匹配就命中目標(biāo)
可以定義結(jié)構(gòu)MyStruct如下:
struct MyStruct
{
??? int key1;
??? double key2
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? /* 兩個(gè)key任意一個(gè)匹配就命中 */
??????? return ( ( key1 < rhs.key1 || (key1 > rhs.key1 && key2 < rhs.key2 ) ) && ( key2 < rhs.key2 ) );
?? }
};(六) 如果被存儲(chǔ)的T允許重復(fù),可用multimap(七) 如果Key本身就是需要被存儲(chǔ)的T, 只要將map換成set就好了

轉(zhuǎn)載于:https://www.cnblogs.com/shenyanyun/archive/2010/05/19/1739308.html

總結(jié)

以上是生活随笔為你收集整理的std::map用法总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。