1. 程式人生 > >關聯容器map(紅黑樹,key/value)

關聯容器map(紅黑樹,key/value)

數值 logs items image 劃線 tor tar 參數 cde

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

字符串或串(String)是由數字、字母、下劃線組成的一串字符。一般記為 s=“a1a2···an”(n>=0)。它是編程語言中表示文本的數據類型。在程序設計中,字符串(string)為符號或數值的一個連續序列,字符串在存儲上類似字符數組,所以它每一位的單個元素都是可以提取的,如s=“abcdefghij”,則s[1]=“a”,s[10]="j"

//線性序列,裏面只存了一個元素,map存的是鍵值對

vector [2,3,4,5] 操作,插入刪除 查找效率

list [1->2->3->4->5] 操作,插入刪除效率高, 查找低

雙端隊列 [2,3,4,9,11,22,33] 操作,頭尾插入刪除效率高,查找高

串 s = “a1 a2 a3 a4 a5”

技術分享技術分享


#include <iostream> #include <ctime> #include <map> #include<algorithm> using namespace std; typedef struct itemstruct{ int a; char b[20]; }itemS; itemS s[4]={{98,"what"}, {88, "
hello"}, {98,"world"}, {77, "c++"} }; int main() { map<string,itemS> mymap; string str[4] = {"1st","2nd","3rd","4th"}; for(int i = 0; i<4; i++) { //first是Key, second是value; mymap.insert(make_pair(str[i], s[i])); } map
<string,itemS>::iterator it; for(it = mymap.begin(); it!=mymap.end(); it++) { //first是Key, second是value; cout<<it->first<<" "<<it->second.a<<" "<<it->second.b<<endl; } map<string,itemS>::iterator iter; iter = mymap.find("4th"); cout << iter->second.b; /*查找map中是否包含某個關鍵字條目用find()方法, 返回數據所在位置的叠代器 傳入的參數是要查找的key*/ return 0; }

技術分享技術分享

關聯容器map(紅黑樹,key/value)