博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ code Summary --- 2015.11.8
阅读量:7105 次
发布时间:2019-06-28

本文共 3359 字,大约阅读时间需要 11 分钟。

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
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
= '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 !"<
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 " <

 

   

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

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

转载地址:http://svjhl.baihongyu.com/

你可能感兴趣的文章
vi 常用命令行
查看>>
优酷去掉广告代码
查看>>
iptables简单设置
查看>>
《Java从入门到放弃》JavaSE入门篇:面向对象语法一(入门版)
查看>>
我的友情链接
查看>>
京东自建物流寓意电商进入蜕化阶段
查看>>
Kali Linux 2016.2发布提供虚拟机以及系统镜像下载
查看>>
关于配置
查看>>
2017年开工啦
查看>>
使用Python向MySQL数据库中存入json类型数据
查看>>
响应式微服务 in java 译 <十八> Deploying a Microservice in OpenShift
查看>>
RHEL5安装Oracle10gRAC on VMware Server1.0之三
查看>>
使用check_openmanage监控win主机的一些常见写法
查看>>
ALV中背景和字体颜色改变的做法
查看>>
new 运算符
查看>>
使用wsimport生成本地代码查看天气预报
查看>>
通过Puppet角色提取配置文件,推送到另一台服务器上
查看>>
bash变量子串的常用操作
查看>>
Kali 2.0安装之后需要做的事——使用SSH进行远程登录
查看>>
高阶函数、闭包、匿名函数
查看>>