STL學習之map與multimap操作練習

2021-08-13 02:05:45 字數 1464 閱讀 7682

// stl_map.cpp: 定義控制台應用程式的入口點。

///* map為標準關聯式容器,乙個map是乙個鍵值對序列,即(key,value)對,提供基於key的快速檢索能力

map的鍵值唯一,集合中元素按一定順序排列,元素插入過程按順序規則插入,不能指定插入位置

map可以直接取key所對應的value,支援操作符

multimap與map的區別,map支援唯一鍵值,每個鍵只能出現一次;而multimap中相同鍵可以出現多次。multimap不支援操作符

wangsl */

#include "stdafx.h"

#include #include #include#includeusing namespace std;

/* 增刪改查 wangsl */

void map_test()

/* 查詢 wangsl */

map::iterator it2 = map1.find(100);

if (it2 == map1.end())

else

cout<< it2->first <<"\t"/* 1. >=5的位置 2. >5的位置 wangsl */

pair::iterator,map::iterator> mypair = map1.equal_range(5);

if (mypair.first == map1.end())

else

cout<< mypair.first->first<<"\t"if (mypair.second == map1.end())

else

cout<< mypair.second->first << "\t" << mypair.second->second << endl;

/* 刪除 wangsl */

while (!map1.empty()) }

///* multimap

乙個key可以對應多個value

wangsl */

/* 員工資訊 wangsl */

class person

public:

string name;

int age;

string tel;

double salary; };

void multimap_test()

int num = map1.count("development");

int tag = 0;

multimap::iterator it = map1.begin();

//it = map1.find("王一"); /* find()只能找key值 wangsl */

it = map1.find("development");

while ( it!= map1.end() && tag < num) }

int main()

~

日常學習 STL之map

對map最初的印象,源於王建德老師在山東noip夏令營上那銷魂的一句 map,i,j 音 嘜頗,挨,傑 ruka上對map並沒有講很多,ty菌說map主要用來寫雜湊 雖然她曾用它寫過鄰接矩陣 迴圈巢狀map intorz 而李晨和里奧甚至表示基本不用map。但是既然有這一項奇妙的東西,想必是有用的,...

STL之map學習例項

1 2 include3 include4 include5 include6 include7 include8 include9 using namespace std 1011 define sale depatment 1 銷售部門 12 define develop depatment 2...

map 初步學習STL

map get allocator include include includeusing namespace std int main int psize mapmymap pair p allocate an array of 5 elements using mymap s allocato...