开发平台(Platform): (Ex: Win10, Linux, ...) Win8.1 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) GCC 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) NO 问题(Question): 我最近PYTHON刚转入C++ 虽说知道C++文字处理相较PYTHON困难 但还是想投入,希望能上手 我目前卡住了 我想把INPUT 变成 OUTPUT那种格式 目前已经把INPUT的资料读档丢进vector里 想要从Vector中,去处理文字 我想要像output那样 每一行第一个数字显示这一行共有几组编码 例如 1 xy 2 , xy 2 就是一组编码 又如 5 R 1004 I 5678 E 2000 R 8002 E 7001 R 1004 / I 5678 / E 2000 / R 8002 / E 7001 各自都是一组编码 但目前发现,空白也是vector一个element,单一的数字也是element,xy中x和y各自都是 独立的element 想要请问一下如何在vector中,只要element之间没有space,就合并成一个element 例如 x和y合并 / 1 0 0 4 合并成1004 而且又要排成output的格式 也就是每个文字之间都空一格 喂入的资料(Input): 1 xy 2 2 z xy 5 R 1004 I 5678 E 2000 R 8002 E 7001 0 1 z 6 R 8001 E 1000 E 1000 E 3000 R 1002 A 1010 0 1 z 2 R 5001 E 4000 1 z 2 2 xy z 3 A 8000 E 1001 E 2000 预期的正确结果(Expected Output): 1 xy 2 2 z xy 5 R 1004 I 5678 E 2000 R 8002 E 7001 0 1 z 6 R 8001 E 1000 E 1000 E 3000 R 1002 A 1010 0 1 z 2 R 5001 E 4000 1 z 2 2 xy z 3 A 8000 E 1001 E 2000 错误结果(Wrong Output): 目前有跑过iterator,内圈用while loop,直接前面合并后面 但是code不合法 上网找范例找破头 如果是一开始就不该存进vector的话,也麻烦不吝指教,我想学习判断如何用正确的方式 储存资料 程式码(Code):(请善用置底文网页, 记得排版) #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { // Open our file ifstream inFile("inputfile",ifstream::in); // If we can read/write great if (inFile.good()) { // Create a vector of strings std::vector<string> strVector; // Create a string variable to hold each line read from the file. string line; // Read throuthe file and push onto our vector while (!inFile.eof()) { getline(inFile,line); strVector.push_back(line); } // combine character together // create a vector iterator vector<string>::iterator iterate_inFileLine; // loop through the vector from start to end for (iterate_inFileLine = strVector.begin(); iterate_inFileLine < strVector.end(); iterate_inFileLine++){ while (iterate_inFileLine !=""){ strVector[iterate_inFileLine] = strVector[iterate_inFileLine] + strVector[iterate_inFileLine + 1]; } } /* // Create a vector iterator vector<string>::iterator it; // Loop through the vector from start to end and print out the string // the iterator is pointing to. for (it = strVector.begin(); it < strVector.end(); it++) { cout << *it << endl; } */ } inFile.close(); system("pause"); return 0; } 补充说明(Supplement):