您的位置:首页 > 编程语言 > C语言/C++

C++ code Summary --- 2015.11.8

2015-11-08 09:31 399 查看
C++ code summary


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

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

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

}

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

统计输入中每一个单词出现的次数(排除常见单词,如:”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");

}

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

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


   分为3步骤:

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

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

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

#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>

using namespace std;

string &trans(string &s)
{
for (int p=0; p<s.size(); p++){
if (s[p] >= 'A' && s[p] <= 'Z')
s[p] -= ('A'-'a');
else if (s[p] == ',' || s[p] == '.')
s.erase(p,1);
}

return s;
}

int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in){
cout<<"failed to open file !"<<endl;
exit(1);
}

map<string, size_t> word_count;
string word;
while(in>>word)
++word_count(trans(word));

for (const auto &w:word_count)
cout<< w.first << " occurs " << w.second << " times " <<endl;

return 0;
}


   

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

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

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

}

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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: