企業(yè)網(wǎng)站的缺點有域名了怎么建立網(wǎng)站
【STL map 簡介】
●?STL map 是一種關(guān)聯(lián)容器,存儲鍵值對,每個鍵(key value)是唯一的,而值(mapped value)可以重復(fù)。構(gòu)建 STL map 時,無論元素插入順序如何,STL map 中的元素始終按“鍵值”自動遞增存儲。STL map 中的迭代器可理解為“指針”。
#include <bits/stdc++.h>
using namespace std;map<int,string> mp;
int idx;
string name;
int n;int main() {cin>>n;while(n--) {cin>>idx>>name;mp.insert({idx,name});}for(auto it=mp.begin(); it!=mp.end(); it++) {cout<<it->first<<":"<<it->second<<endl;}return 0;
}/*
in:
5
3 Java
1 C++
9 Python
8 R
6 SQLout:
1:C++
3:Java
6:SQL
8:R
9:Python
*/
● STL map 常用函數(shù)的功能與 STL set 常用函數(shù)的功能基本一致。
(1)lower_bound(x):返回一個迭代器,該迭代器指向第一個大于等于 x 的元素。若無,返回 end() 得到的迭代器。
(2)upper_bound(x):返回一個迭代器,該迭代器指向第一個大于 x 的元素。若無,返回 end() 得到的迭代器。
(3)find(x):返回一個迭代器,該迭代器指向“鍵 x”所在的元素。
(4)erase(first, last):從 map 中刪除迭代器 first 及迭代器 last 指向的區(qū)間 [first, last) 中的元素(注意:左閉右開)。
#include <bits/stdc++.h>
using namespace std;int main() {map<int,string> mp= {{9,"a"}, {7,"b"}, {2,"c"}, {6,"d"}};auto first=mp.find(2);auto last=mp.find(7);mp.erase(first,last);for(auto it=mp.begin(); it!=mp.end(); it++) {cout<<it->first<<":"<<it->second<<endl;}return 0;
}/*
output:
7:b
9:a
*/
(5)begin():返回一個迭代器,該迭代器指向 map 第一個元素的位置。
(6)end():返回一個迭代器,該迭代器指向 map 最后一個元素的下一個位置。
(7)count(x):返回 map 中“鍵”?x 的個數(shù)。
#include <bits/stdc++.h>
using namespace std;int main() {map<int,string> mp= {{6,"apple"},{8,"banana"}};cout<<mp.count(6)<<endl; //output 1cout<<mp.count(5)<<endl; //output 0return 0;
}
由于 map?中所有“鍵”的個數(shù)都是唯一的,所以若 x 在 map 中,返回 1,若 x 不在 map 中,返回 0。
【參考文獻】
https://cplusplus.com/reference/map/map/
https://cplusplus.com/reference/map/map/count/
https://cplusplus.com/reference/map/map/lower_bound/
https://cplusplus.com/reference/map/map/upper_bound/
https://cplusplus.com/reference/map/map/insert/
https://cplusplus.com/reference/map/map/erase/
https://cplusplus.com/reference/map/map/begin/
https://www.cnblogs.com/linxiaoxu/p/17694869.html
?