国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

C++關(guān)聯(lián)容器(unordered_map,vector,unordered_set)

2019-11-06 06:31:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

說明書: http://classfoo.com/ccby/article/qNNOJ#sec_4Gxme0

unordered_set:(無(wú)序集合)

//500class Solution {public: vector<string> findWords(vector<string>& words) { vector<string> res;//來(lái)一個(gè)string類的動(dòng)態(tài)數(shù)組 unordered_set<char> row1{'q','w','e','r','t','y','u','i','o','p'};//存儲(chǔ)鍵盤上第一行的字母 unordered_set<char> row2{'a','s','d','f','g','h','j','k','l'};//存儲(chǔ)鍵盤上第二行的字母 unordered_set<char> row3{'z','x','c','v','b','n','m'};//存儲(chǔ)鍵盤上第三行的字母 for (string word : words) //for循環(huán)數(shù)組words,words的元素就是一個(gè)單詞 { int one = 0, two = 0, three = 0;//初始化計(jì)數(shù)器 for (char c : word) {//for循環(huán)數(shù)組word(一個(gè)單詞),word的元素即是字符 if (c < 'a') c += 32; if (row1.count(c)) one = 1;//count函數(shù)返回的是符合的元素的個(gè)數(shù),如果沒有,則返回0,即false,大于0則為true if (row2.count(c)) two = 1; if (row3.count(c)) three = 1; if (one + two + three > 1) break;//即這個(gè)單詞只用一行是無(wú)法輸入完的 } if (one + two + three == 1) res.push_back(word); } return res; }};

**成員函數(shù): find 通過給定主鍵查找元素 count 返回匹配給定主鍵的元素的個(gè)數(shù)//見上例子 equal_range 返回值匹配給定搜索值的元素組成的范圍**

vector(順序容器)

//詳解:http://blog.csdn.net/QQ_32175379/article/details/60469987說白了就是定義動(dòng)態(tài)數(shù)組的

unordered_map(無(wú)序映射表)

//496//建立哈希表用class Solution {public: vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) { vector<int> res(findNums.size()); unordered_map<int, int> m;//建立哈希表,將值與索引建立聯(lián)系 for (int i = 0; i < nums.size(); ++i) { m[nums[i]] = i;//例如將nums數(shù)組的值與索引建立關(guān)系,這個(gè)m數(shù)組不是一般的數(shù)組,自行畫圖理解 } for (int i = 0; i < findNums.size(); ++i) { res[i] = -1; int start = m[**findNums[i]**];//這個(gè)可以直接定位findNums中元素的值在nums中的位置 for (int j = start + 1; j < nums.size(); ++j) { if (nums[j] > findNums[i]) { res[i] = nums[j]; break; } } } return res; }};

成員函數(shù): begin 返回指向容器起始位置的迭代器(iterator) end 返回指向容器末尾位置的迭代器 cbegin 返回指向容器起始位置的常迭代器(const_iterator) cend 返回指向容器末尾位置的常迭代器 Capacity:

size 返回有效元素個(gè)數(shù) max_size 返回 unordered_map 支持的最大元素個(gè)數(shù) empty 判斷是否為空 Element access:

Operator[] 訪問元素 at 訪問元素 Modifiers:

insert 插入元素 erase 刪除元素 swap 交換內(nèi)容 clear 清空內(nèi)容 emplace 構(gòu)造及插入一個(gè)元素 emplace_hint 按提示構(gòu)造及插入一個(gè)元素 Observers:

hash_function 返回 hash 函數(shù) key_eq 返回主鍵等價(jià)性判斷謂詞 Operations:

find 通過給定主鍵查找元素 count 返回匹配給定主鍵的元素的個(gè)數(shù) equal_range 返回值匹配給定搜索值的元素組成的范圍 Buckets:

bucket_count 返回槽(Bucket)數(shù) max_bucket_count 返回最大槽數(shù) bucket_size 返回槽大小 bucket 返回元素所在槽的序號(hào)


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 伊吾县| 忻州市| 威信县| 金湖县| 都江堰市| 慈溪市| 伊金霍洛旗| 松江区| 榆中县| 疏附县| 论坛| 驻马店市| 拉萨市| 达日县| 洪泽县| 抚州市| 玛纳斯县| 海原县| 余姚市| 从化市| 蕉岭县| 台中县| 怀化市| 鹤岗市| 渝中区| 鄂温| 阜新市| 织金县| 尼勒克县| 吉木萨尔县| 沐川县| 大渡口区| 赣州市| 睢宁县| 绩溪县| 曲阳县| 乌海市| 合山市| 西乌| 丰都县| 沁阳市|