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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

map的用法

發布時間:2023/11/30 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 map的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/* ACM中map的基本的用法,主要是用數組的形式實現。 1.構造map 2.數據插入 3.map的大小 4.數據的查找 5.數據的刪除 */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<map> using namespace std; int main() {map<int,string>m;//構造mapm.clear();//數據清空m[1] = "abc";//數據插入m[2] = "def";m[3] = "ghi";cout<<m.size()<<endl;//輸出map的大小if(m[1].size()) cout<<m[1]<<endl;//先判斷該數據是否存在,存在就輸出cout<<m[4].size()<<endl;//不出在輸出0int n = m.erase(1);//刪除元素,成功的話返回1return 0; } //如果map的映射值類型為字符類型,那么輸出這個映射值不能用printf();輸出,會報錯,cout就行。 /* lower_bound(),upper_bound()函數介紹 */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include <algorithm> #include<map> using namespace std; int a[10] = {2,3,4,5,6,7,8,9}; int main() {int n = 8;//數組長度int pos = lower_bound(a,a+n,5)-a;//返回大于等于5的那個數在數組中的地址cout<<a[pos]<<endl;pos = upper_bound(a,a+n,5) -a;//返回大于5的那個數在數組中的地址cout<<a[pos]<<endl;pos = lower_bound(a,a+n,100)-a;//返回的是一個越界的數值,返回8cout<<pos<<endl;return 0; }


#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef struct tt//重載排序
{
int id;
string name;
bool operator < (const tt &a) const{
return a.id>id;
}
}; //學生信息
int main()
{
int nSize;
//用學生信息映射分數
map<tt, int>m;
map<tt, int>::iterator iter;
tt s;
s.id = 2;
s.name = "Jack";
m[s] = 100;
s.id = 1;
s.name = "Rose";
m[s] = 100;
for (iter=m.begin(); iter!=m.end(); iter++)
cout<<iter->first.id<<" "<<iter->first.name<<" "<<iter->second<<endl;
}

轉載于:https://www.cnblogs.com/llei1573/p/3867469.html

總結

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

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