读书笔记(C++)——【关联容器】

    xiaoxiao2024-11-21  2

    #include<iostream> #include<fstream> #include<sstream> #include<string> #include<cctype> #include<vector> #include<map> #include<set> #include <stdexcept> #include<stdlib.h> using namespace std; //map //表10 - 5 insert 1,m.insert(e); 2,m.insert(beg,end); 3,m.insert(iter,e) //10.3.4//使用下标访问 map<string,int> word_count; string word; while(cin>>word) ++word_count[word]; word_count.insert(make_pair("Anna",1)); or typedef map<string,int>::value_type calType; word_count(valuType("Anna",1)); //10.3.5 map insert使用 map<string,int> word_count; while(cin>>word) { pair<map<string,int>::iterator,bool> ret = word_count.insert(make_pair(word,1)); if(!ret.second) ++ret.first->second; / } //表10 — 6 不修改map对象的查询操作 m.count(k); m.find(k); int occurs = 0; map<string,int>::iterator it = word_count.find("foobar"); if(it != word_count.end()) occurs = it->second; //10.3.7 map中删除对象 //表10 - 7 m.erase(k);//返回1 or 0 m.erase(p);//p为迭代器; m.erase(b,e); //10.3.8 遍历 map<string,int>::iterator map_it = word_count.begin(); while(map_it != word_count.end()) { cout<<map_it->first<<":"<<map_it->second<<endl; ++map_it; } //10.3.9 单词转换 int main(int argc,char **argv) { map<string,int> trans_map; string key,value; if(argc != 3) throw runtime_error("wrong number of arguments"); ifstream map_file; if(!open_file(map_file,atgv[1])) throw runtime_error("no transformation file"); while(map_file>>key>>value) trans_map.insert(make_pair(key,value)); ifstream input; if(!open_file(input,argv[2])) throw runtime_error("no input file"); while(getline(input,line)) { istringstream stream(line); string word; bool firstword = true; while(stream>>word) { map<string,string>::const_iterator map_it = trans_map.find(word); if(map_it != trans_map.end()) word = map_it->second; if(firstword) firstword = false; else cout<<" "; cout<<word; } cout<<endl; } } //10.4.1 set定义和使用 vector<int> ivec; for(int i = 0;i != 10;i++) { ivec.push_back(i); ivec.push_back(i); } set<int> iset(ivec.begin(),ivec.end()) cout<<ivec.size()<<endl;//prints 20; cout<<iset.size()<<endl;//prints 10; //在set中添加元素 set<string> set1; set1.insert("the"); set1.insert("and"); or set<int> set2; set2.insert(ivec.begin(),ivec.end()); //获取元素 iset.find(1)//returns iterator that refers to the element with the key == 1 iset.find(11)//returns iterator == iset.end(); iset.count(1)//returns 1; iset.count(11)//returns 0; set<int>::iterator set_it; = iset.find(1); *set_it = 11;//error:keys in a set are read_only; cout<<*set_it<<endl; //10.4.2 单词排除集 void restricted_wc(ifstream &remove_file, map<string,int> &cord_count) { set<string> excluded; string remove_word; while(remove_file>>remove_word) excluded.insert(remove_word); string word; while(cin>>word) if(!excluded.count(word)) ++word_count[word]; } //10.5 multimap 和 multiset //10.5.1 元素添加和删除 authors.insert(make_pair( string("Barth,John") string("Sot-Weed Factor"))); authors.insert(make_pair( string("Barth,John") string("Lost in the Funhouse"))); multimap<string,string> authors; string search_item("Kazuo Ishiguro"); multimap<string,string>::size_type cnt = authors.erase(search_item); //在multimap和multiset中查找元素 //1.使用find和count操作 string search_item("WG"); typedef multimap<string,string>::size_type sz_type; sz_type entrise = authors.count(search_item); multimap<string,string>::iterator iter = authors.find(search_item); for(sz_type cnt = 0;cnt != entrise;++cnt,++iter) cout<<iter->second<<endl; //2.面向迭代器解决 //表10 - 8 返回迭代器的关联容器操作 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) // typedef multimap<string,string>::iterator authors_iter; authors_iter beg = authors.lower_bound(search_item); _end = authors.upper_bound(search_item); while(beg != _end) { cout<<beg->second<<endl; ++beg; } //直接调用equal_range解决 pair<authors_iter,authors_iter> pos = authors.equal_range(search_item); while(pos.first != pos.second) { cout<<pos.first->second<<endl; ++pos.first; } //容器综合应用:文本查询程序 #include<iostream> #include<fstream> #include<sstream> #include<string> #include<cctype> #include<vector> #include<map> #include<set> #include <stdexcept> #include<stdlib.h> 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) { std::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) { std::istringstream line(lines_of_text[line_num]); std::string word; while(line>>word) word_map[word].insert(line_num); } } std::set<TextQuery::line_no> TextQuery::run_query(const std::string &query_word) const { std::map<std::string,std::set<line_no> >::const_iterator loc = word_map.begin(); if(loc == word_map.end()) return std::set<line_no>(); else return loc->second; } std::string TextQuery::text_line(line_no line)const { if(line<lines_of_text.size()) return lines_of_text[line]; throw std::out_of_range("line number out of range"); } void print_results(const std::set<TextQuery::line_no>& locs, const std::string& sought,const TextQuery &file); std::ifstream& open_file(std::ifstream &in,const std::string &file) { in.close(); in.clear(); in.open(file.c_str()); return in; } std::string make_plural(size_t ctr,const std::string &word,const std::string &ending) { return (ctr==1)?word:word + ending; } int main(int argc,char **argv) { std::ifstream infile; if(argc < 2 || !open_file(infile,argv[1])) { std::cerr<<"No input file!"<<std::endl; return EXIT_FAILURE; } TextQuery tq; tq.read_file(infile); while(true) { std::cout<<"enter word to look for,or q to quit: "; std::string s; std::cin>>s; if(!std::cin || s == "q") break; std::set<TextQuery::line_no> locs = tq.run_query(s); print_results(locs,s,tq); } return 0; } void print_result(const std::set<TextQuery::line_no>& locs, const std::string& sought,const TextQuery &file) { typedef std::set<TextQuery::line_no> line_nums; line_nums::size_type sz = locs.size(); std::cout<<"\n"<<std::cout<<sought<<" occurs"<<sz<<" " <<make_plural(sz,"time","s")<<std::endl; line_nums::const_iterator it = locs.begin(); for(;it != locs.end();++it) { std::cout<<"\t(line " <<(*it) + 1<<") " <<file.text_line(*it)<<std::endl; } }
    转载请注明原文地址: https://ju.6miu.com/read-1293865.html
    最新回复(0)