C++ code Summary --- 2015.11.8

C++ code summary

 

  1. map<int, PersonClassifier>::iterator itmap<int, PersonClassifier> it的区别与联系


-----------------------------------------------------------------------------------

  1. 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)

-----------------------------------------------------------------------------------


  1. 对输入的句子,统计每一个单词出现的次数:

int main() {

map<string, 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");

}

-----------------------------------------------------------------------------------

  1. 统计输入中每一个单词出现的次数(排除常见单词,如:”the”, ”a”, ”an”等等)

#include<iostream>

#include<string>

#include<map>

#include<set>


usingnamespace std;


int main() {

map<string, size_t> word_count;

set<string> 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");

}

-----------------------------------------------------------------------------------

  1. 忽略大小写和标点,如:”example,” “example.” “Example”应该递增相同计数器。

   分为3步骤:

   1.先将所有的大写字母改为小写;

   2.将所有的标点符号都删去;

   3.将转换好的字符串返回.

 

 1 #include<iostream>
 2 #include<fstream>
 3 #include<map>
 4 #include<string>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 string &trans(string &s)
10 {
11     for (int p=0; p<s.size(); 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 !"<<endl;
26         exit(1);
27     }
28 
29     map<string, size_t> word_count;
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 " <<endl;
36 
37     return 0;
38 }

 

   


-----------------------------------------------------------------------------------

  1. 对关联容器进行值初始化:

map<string, size_t> word_count;

set<string> exclude = {“the”, ”but”, “and”};


map<string, string> authers = { {“wang”, “xiao”}, {“wang”, ”kui”}, {“wang”, ”jianjun”} };


#include<iostream>

#include<string>

#include<map>

#include<set>

#include<vector>


usingnamespace std;


int main() {


vector<int> ivec;

for (vector<int>::size_type i = 0; i != 20; ++i) {


ivec.push_back(i);

ivec.push_back(i);

}


set<int> iset(ivec.cbegin(), ivec.end());

multiset<int> miset(ivec.cbegin(), ivec.cend());


cout << ivec.size() << endl;

cout << iset.size() << endl;

cout << miset.size() << endl;


system("pause");

}


  1. pair类型:

一个pair保存两个数据成员,pair是一个用来生成特定类型的模板。

pair<string, string> anon;

pair<string, size_t> word_count;

pair<string, vector<int>> line;

pair<string, string> auther {“wangxiao”, “wangkui”};


      pair类型的数据成员是public的,两个成员分别为 first second

 

 

       9. ----

#include<iostream>
#include<string>
#include<map>
 
using namespace std;
 
int main() {
    map<string, size_t> word_count;
    string word;
    while (cin >> word) {
        ++word_count[word];
    }
        
    for (const auto &w : word_count)
        cout << w.first << "occurs" << w.second  
             << ((w.second > 1) ? "times" : "time") << endl;
 
    system("pause");
}















上一篇:论文阅读之 Inferring Analogous Attributes CVPR 2014


下一篇:视觉AI训练营——Day1