刪除特定位置前面的字串c++程式碼例項及執行結果
阿新 • • 發佈:2019-02-08
原始字串樣式
c++程式碼
執行結果#include<iostream> #include<string> #include<vector> #include<fstream> using namespace std; int main() { ifstream path("E:\\vs2013\\opencv_code\\GTSRBtrafficSign\\val.txt");//讀取初始的字串位置在val.txt檔案中 ofstream onlyLabel("E:\\vs2013\\opencv_code\\GTSRBtrafficSign\\test\\onlyLabel.txt");//提取的標籤儲存在onlyLabel.txt檔案中 vector<string> pathAndLabel;//暫時儲存原始字串 string stringLabel;//只有標籤的字串 char line[100]; string buf; while (path) { getline(path, buf); pathAndLabel.push_back(buf);//原始字串儲存在pathAndLabel中 } for (string::size_type i = 0; i < pathAndLabel.size(); i++) { stringLabel = pathAndLabel[i].c_str(); stringLabel = stringLabel.erase(0, 10);//erase的用法不熟可百度,這裡是從第一個字元開始刪一直刪到第10個字元 sprintf_s(line, stringLabel.c_str()); cout << line <<endl; onlyLabel << atoi(line)<<endl;//atoi將字元轉換成整形 } system("pause"); }