C++ code Summary --- 2015.11.8

简介: C++ code summary   map::iterator it与 map it的区别与联系 --------------------------------------------------------------------------...

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















相关文章
|
18天前
|
人工智能 NoSQL 机器人
【C++】VS code如何配置使用C++(手把手教学)
【C++】VS code如何配置使用C++(手把手教学)
|
2月前
|
C语言 C++
VScode中C++多文件编译运行问题(使用code runner配置)
VScode中C++多文件编译运行问题(使用code runner配置)
|
4月前
|
Ubuntu Linux 开发工具
Linux操作系统Ubuntu 22.04配置Visual Studio Code与C++代码开发环境的方法
Linux操作系统Ubuntu 22.04配置Visual Studio Code与C++代码开发环境的方法
128 0
|
8月前
|
Ubuntu Linux 网络安全
使用VS Code 配置Ubuntu远程C++开发环境
1.在Ubuntu 中配置ssh远程登录 Ubuntu 配置远程登录 2.VsCode 安装 Remote-ssh 插件
205 0
|
10月前
|
Java C++ iOS开发
macos上VS Code上配置Python、Java、C++环境变量
macos上VS Code上配置Python、Java、C++环境变量
187 0
macos上VS Code上配置Python、Java、C++环境变量
|
JSON Ubuntu Linux
Linux Ubuntu配置Visual Studio Code及C++环境的方法
本文介绍在Linux Ubuntu操作系统下,配置Visual Studio Code软件与C++代码开发环境的方法~
1555 1
Linux Ubuntu配置Visual Studio Code及C++环境的方法
|
Java C语言 C++
0基础都能看懂的 Visual Studio Code(VScode)使用脚本一键配置安装C/C++环境、编译运行Windows版本教程(内附脚本、安装包下载链接)
网上很多配置VScode的C、C++环境的教程,但是很多时候跟着从头到尾做了之后反而还是运行不了,于是笔者在网上翻阅资料后,发现了一个自动配置环境的脚本,亲测有效,大概5分钟就可以配置好环境了。直接进入教程。
0基础都能看懂的 Visual Studio Code(VScode)使用脚本一键配置安装C/C++环境、编译运行Windows版本教程(内附脚本、安装包下载链接)
|
JavaScript 前端开发 Linux
基于Windows和WSL 2的Visual Studio Code (VS Code) 安装及搭建 C/C++ 编译环境完整详细步骤
基于Windows和WSL 2的Visual Studio Code (VS Code) 安装及搭建 C/C++ 编译环境完整详细步骤
366 0
基于Windows和WSL 2的Visual Studio Code (VS Code) 安装及搭建 C/C++ 编译环境完整详细步骤
|
JSON 编译器 Go
VS CODE一些常见配置操作(快捷键设置、C/C++的debug、代码路径配置)
VS CODE一些常见配置操作(快捷键设置、C/C++的debug、代码路径配置)
549 1
VS CODE一些常见配置操作(快捷键设置、C/C++的debug、代码路径配置)