#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<set>
#include <stdexcept>
#include<stdlib.h>
using namespace std;
1,m.insert(e);
2,m.insert(beg,end);
3,m.insert(iter,e)
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));
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;
/ }
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;
m.erase(k);
m.erase(p);
m.erase(b,e);
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;
}
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;
}
}
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;
cout<<iset.size()<<endl;
set<string> set1;
set1.insert(
"the");
set1.insert(
"and");
or
set<int> set2;
set2.insert(ivec.begin(),ivec.end());
iset.find(
1)
iset.find(
11)
iset.count(
1)
iset.count(
11)
set<int>::iterator set_it; = iset.find(
1);
*set_it =
11;
cout<<*set_it<<endl;
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];
}
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);
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;
m.lower_bound(K)
m.upper_bound(K)
m.equal_range(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;
}
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