p { margin-bottom: 0.1in; direction: ltr; line-height: 120%; text-align: justify }
a:link { }
C++ code summary
map
-----------------------------------------------------------------------------------
C++
并行程序的执行:
int
coreNum = omp_get_num_procs();
#pragma
omp parallel for
num_threads(coreNum) default(none)
\
private(po_num,
it, queryLabel) \
shared(al_random_select_num,
satisfy_size, satisfy_classifier_label, not_selected, random_index)
-----------------------------------------------------------------------------------
对输入的句子,统计每一个单词出现的次数:
int
main() {
map size_t> word_count; string word; while (cin >> word) { ++word_count[word]; } for (constauto &w : word_count) cout << w.first <<"occurs"<< w.second << ((w.second > 1) ? "times" : "time") << endl; system("pause"); } ----------------------------------------------------------------------------------- 统计输入中每一个单词出现的次数(排除常见单词,如:”the”, ”a”, ”an”等等) #include #include #include #include usingnamespace std; int main() { map size_t> word_count; set exclude = {"the", "a", "an"}; string word; while (cin >> word) if (exclude.find(word) == exclude.end()) ++word_count[word]; for (constauto &w : word_count) cout << w.first <<" occurs "<< w.second <<" times "<< endl; system("pause"); } ----------------------------------------------------------------------------------- 忽略大小写和标点,如:”example,” “example.” “Example”应该递增相同计数器。 分为3步骤: 1.先将所有的大写字母改为小写; 2.将所有的标点符号都删去; 3.将转换好的字符串返回. 1 #include 2 #include 3 #include 4 #include 5 #include 6 7 using namespace std; 8 9 string &trans(string &s) 10 { 11 for (int p=0; p 12 if (s[p] >= 'A' && s[p] <= 'Z') 13 s[p] -= ('A'-'a'); 14 else if (s[p] == ',' || s[p] == '.') 15 s.erase(p,1); 16 } 17 18 return s; 19 } 20 21 int main(int argc, char *argv[]) 22 { 23 ifstream in(argv[1]); 24 if (!in){ 25 cout<<"failed to open file !"< 26 exit(1); 27 } 28 29 map 30 string word; 31 while(in>>word) 32 ++word_count(trans(word)); 33 34 for (const auto &w:word_count) 35 cout<< w.first << " occurs " << w.second << " times " < 36 37 return 0; 38 } ----------------------------------------------------------------------------------- 对关联容器进行值初始化: map set map {“wang”, ”kui”}, {“wang”, ”jianjun”} }; #include #include #include #include #include usingnamespace std; int main() { vector ivec; for (vector i = 0; i != 20; ++i) { ivec.push_back(i); ivec.push_back(i); } set iset(ivec.cbegin(), ivec.end()); multiset miset(ivec.cbegin(), ivec.cend()); cout << ivec.size() << endl; cout << iset.size() << endl; cout << miset.size() << endl; system("pause"); } pair类型: 一个pair保存两个数据成员,pair是一个用来生成特定类型的模板。 pair pair pair pair pair类型的数据成员是public的,两个成员分别为 first 和 second。 9. ---- #include 相关链接
发表评论