說明書: 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)