C++ 实用的小程序

1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt

 1 #include <iostream>
 2 #include <string>
 3 #include <fstream>
 4 #include <sstream>
 5 #include <iostream>
 6 using namespace std;
 7 int main(){
 8 
 9     ifstream file;
10     file.open("/home/wangxiao/Downloads/Link to ASPL---code/features/test_ids.txt");
11     ofstream in("/home/wangxiao/Downloads/test_ids_repaired.txt");
12 
13     std::string line;
14     std::string image_id;
15 
16     while(getline(file, line)){
17         std::string add = "1_";
18         line = add + line;
19 
20         image_id = line;
21         in<<image_id<<endl;
22     }
23  }

 

 

 

 

2 : 编写一个程序,读入stringint的序列,将每个stringint存入一个pair中,pair保存在一个vector中.

 

 1 #include<iostream>
 2 #include<fstream>
 3 #include<utility>
 4 #include<vector>
 5 #include<string>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 int main(int argc, char *argv[])
10 {
11       ifstream in(argv[1]);
12       if (!in) {
13           cout<<”打开输入文件失败!”<<endl;
14           exit(1);
15       }
16 
17 vector<pair<string, int>> data;
18 string s;
19 int v;
20 while (in>>s && in>>v)
21      data.push_back(pair<string, int>(s, v));
22   // data.push_back({s, v});  列表初始化的方式;
23   // data.push_back(make_pair(s, v)); 或者使用 make_pair ;
24 
25 for (const auto &d : data)
26      cout<<d.first<<” ---> ”<<d.second<<endl;
27 
28    return 0;
29 }

3. 打开test_ids.txt 将里面的东西添加"i_",然后另存为test_ids_repaired.txt  从1~88种 ...


 1 #include <iostream>
 2 #include <string>
 3 #include <fstream>
 4 #include <sstream>
 5 #include <iostream>
 6 using namespace std;
 7 
 8 int main(){
 9 
10     ifstream file;
11     file.open("/home/wangxiao/Downloads/ASPL_matlabFiles/test_ids.txt");
12     ofstream in("/home/wangxiao/Downloads/ASPL_matlabFiles/outPut_test_ids.txt");
13 
14     std::string line;
15     std::string image_id;
16 
17 
18 for (int i=1;i<89;i++){
19 
20    for (int j=1;j<52;j++){
21 
22        getline(file, line);
23        stringstream ss;
24        string s;
25        ss << i;
26        ss >> s;
27        string add = s + "_";
28        image_id = add + line;
29        in<<image_id<<endl;
30 
31    }
32 }
33  }
原文地址:https://www.cnblogs.com/wangxiaocvpr/p/4942328.html