12、C++ Primer 4th 筆記,關聯容器(2)

來源:互聯網
上載者:User

1、map 和 set 容器中,一個鍵只能對應一個執行個體。multimap和multiset類型允許一個鍵對應多個執行個體。其支援的操作分別與map和set的操作相同,只有一個例外,multimap不支援下標運算。set與multiset專門用於儲存鍵。

2、關聯容器 map 和 set 的元素是按順序儲存的。而 multimap 和multset 也一樣。因此,在 multimap 和 multiset 容器中,如果某個鍵對應多個執行個體,則這些執行個體在容器中將相鄰存放。迭代遍曆 multimap 或 multiset 容器時,可保證依次返回特定鍵所關聯的所有元素。

3、在multimap與multiset中尋找元素時,一般有以下三種方法:

1)使用 find 和 count 。count 函數求出某鍵出現的次數,而 find 操作則返回一個迭代器,指向第一個擁有正在尋找的鍵的執行個體。

2)用標準庫提供的迭代器

m.lower_bound(k)

返回一個迭代器,指向鍵不小於 k 的第一個元素

m.upper_bound(k)

返回一個迭代器,指向鍵大於 k 的第一個元素

m.equal_range(k)

返回一個迭代器的 pair 對象它的 first 成員等價於m.lower_bound(k)。而 second 成員則等價於 m.upper_bound(k)

    若該鍵沒有關聯的元素,則 lower_bound 和 upper_bound 返回相同的迭代

器:都指向同一個元素或同時指向 multimap 的超出末端位置。它們都指向在保

持容器元素順序的前提下該鍵應被插入的位置。

4、一個尋找文本的例子

#include "map"#include "set"#include "vector"#include "string"#include "fstream"#include "iostream"#include "sstream"using namespace std;class TextQuery{public:typedef std::vector<std::string>::size_type line_no;void read_file(std::ifstream &is) //讀取檔案,構造內部資料結構{store_file(is);build_map();}std::set<line_no> run_query(const std::string&) const; //執行尋找std::string text_line(line_no) const; //返回找到單詞的該行文本private:void store_file(std::ifstream&); //隱藏檔void build_map(); //將每個單詞與相關聯的行號建立映射std::vector<std::string> lines_of_text; //儲存原檔案的每一行std::map< std::string, std::set<line_no> > word_map; //映射結構};void TextQuery::store_file(std::ifstream &is){string textline;while(getline(is, textline)){lines_of_text.push_back(textline);}}void TextQuery::build_map(){for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num){istringstream line(lines_of_text[line_num]);string word;while (line >> word){word_map[word].insert(line_num);//word_map.insert(make_pair(word, line_num));//word_map.insert(map<std::string, line_no>::value_type(word, line_no));}}}std::set<TextQuery::line_no>TextQuery::run_query(const string &query_word) const{map<string, set<line_no> >::const_iterator loc = word_map.find(query_word);if (loc == word_map.end())return set<line_no>(); //not found, return empty setelse return loc->second;}std::string TextQuery::text_line(line_no line) const{if (line < lines_of_text.size())return lines_of_text[line];elsethrow std::out_of_range("line number out of range");}ifstream& open_file(ifstream &in, const string &file){in.close(); // close in case it was already openin.clear(); // clear any existing errors// if the open fails, the stream will be in an invalid statein.open(file.c_str()); // open the file we were givenreturn in; // condition state is good if open succeeded}string make_plural(size_t ctr, const string &word,   const string &ending){return (ctr == 1) ? word : word + ending;}void printf_resluts(const set<TextQuery::line_no> &locs, const std::string &sought, const TextQuery &file){typedef set<TextQuery::line_no> line_nums;line_nums::size_type size = locs.size();cout << "\n" << sought << "occurs " << size << make_plural(size, "time", "s") << endl;line_nums::const_iterator it = locs.begin();for (; it != locs.end(); it++){cout << "\t(line" << (*it) + 1 << ")" << file.text_line(*it) << endl;}}int main(int argc, char **argv){ifstream infile("C:\\1.txt");/*if (argc < 2 || !open_file(infile, argv[1])){cerr << "No input file" << endl;return EXIT_FAILURE;}*/TextQuery tq;tq.read_file(infile);while (true){cout << "Enter word to look for, or q to quit: ";string s;cin >> s;if (!cin || s == "q")break;set<TextQuery::line_no> locs = tq.run_query(s);printf_resluts(locs, s, tq);}return 0;}

注意上面例子中:

word_map[word].insert(line_num);不能替換成如下兩種形式//word_map.insert(make_pair(word, line_num));//word_map.insert(map<std::string, line_no>::value_type(word, line_no));

因為make_pair的兩個實參必須是具體的值,而不能是變數。

參考:

[1] http://blog.163.com/zhoumhan_0351/blog/static/399542272010227112325224/

http://blog.163.com/zhoumhan_0351/blog/static/39954227201032392545410/

http://blog.163.com/zhoumhan_0351/blog/static/399542272010229828061/

http://blog.163.com/zhoumhan_0351/blog/static/39954227201038102838349/

http://blog.163.com/zhoumhan_0351/blog/static/399542272010396030501/

http://blog.163.com/zhoumhan_0351/blog/static/39954227201039112031737/

http://blog.163.com/zhoumhan_0351/blog/static/39954227201022994439573/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.