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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

std::map用法总结

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 std::map用法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

可以擴展到多鍵


(一) 介紹
特點:
1.map將Key的object和T的Object綁定到一起,因此是一種Pair Associative Container, 表示其value type為 pair。
2.它同時也是Unique Associative Container,表示沒有兩個元素具有相同的Key。
3.它還是一種Sorted Associative Container,因此第三個參數只能是less,greater之類的functor, 相比較而言,
hash table是 equal_to, not_equal_to之類的functor。(二) 基本用法
通過以下范例,可以看出map的一些基本用法: 插入、查找、刪除、遍歷等等。
/* 這個是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) );
???
??? /* 這個是常用的一種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;
}
(三) 當Key是結構時該如何定義結構
比如 Key是結構MyStruct類型, 此時map可以定義如下:
std::map > _map;
其中Compare 缺省是std::less,這里可以不寫,自定義的結構必須實現Compare指定的比較操作,因此自定義結構
MyStruct必須按照如下寫法:struct MyStruct
{
??? int key;
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? return key < rhs.key;
?? }
};
當然也可以實現全局operator <
bool operator < ( const MyStruct lhs, const MyStruct rhs)
{
??? return lhs.key < rhs.key;
}另外,當Compare 是std::greater時,需要實現 operator >(四) 如何實現兩個Key的map, 只有兩個Key都匹配才命中目標
可以定義結構MyStruct如下:
struct MyStruct
{
??? int key1;
??? double key2
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? /* 兩個key必須都匹配才命中 */
??????? return ( key1 < rhs.key1 || key2 < rhs.key2 );
?? }
};(五) 如何實現兩個Key的map, 兩個Key中任意一個匹配就命中目標
可以定義結構MyStruct如下:
struct MyStruct
{
??? int key1;
??? double key2
???
??? bool operator < ( const MyStruct rhs) const
?? {
??????? /* 兩個key任意一個匹配就命中 */
??????? return ( ( key1 < rhs.key1 || (key1 > rhs.key1 && key2 < rhs.key2 ) ) && ( key2 < rhs.key2 ) );
?? }
};(六) 如果被存儲的T允許重復,可用multimap(七) 如果Key本身就是需要被存儲的T, 只要將map換成set就好了

轉載于:https://www.cnblogs.com/shenyanyun/archive/2010/05/19/1739308.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。