目录:
1.从txt中读一行
2. 分割string字符串
=============================================================
1. 从txt中读一行
1 cout<<"input the filename:"<<endl; 3 string filename; 5 cin>>filename; 7 ifstream infile(filename.c_str()); 9 string temp; 11 while(getline(infile,temp)){ 15 cout<<temp<<endl; 17 }
2. 分割string字符串
// vector<string> split(string str, string pattern) { string::size_type pos; vector<string> result; str += pattern; //在最后加上分割类型,扩展字符串以方便操作 int size = str.size(); for (int i = 0; i < size; i++) { pos = str.find(pattern, i); if (pos < size) { string s = str.substr(i, pos - i); result.push_back(s); i = pos + pattern.size() - 1; } } return result; } // 调用: void test() { string str = "/media/michael/F/data/UCF-101/UCF-101/ApplyEyeMakeup/v_ApplyEyeMakeup_g01_c01.avi" ; string pattern = "."; vector<string> result = split(str, pattern); cout << "The result:" << endl; for(int i=0; i<result.size(); i++) { cout << result[i] << endl; } }
时间: 2024-10-12 19:09:55