c++ getline读取避免空行

    xiaoxiao2021-03-25  16

    #include<fstream> #include<iostream> #include<string> #include <vector> using namespace std; vector< string> split(string str, string pattern) { vector<string> ret; if (pattern.empty()) return ret; size_t start = 0, index = str.find_first_of(pattern, 0); while (index != str.npos) { if (start != index) ret.push_back(str.substr(start, index - start)); start = index + 1; index = str.find_first_of(pattern, start); } if (!str.substr(start).empty()) ret.push_back(str.substr(start)); return ret; } int main() { string pattern = " "; vector <string> result; string name; ifstream in("C:/Users/Administrator/Desktop/save_paper.txt"); while (!in.eof()) { getline(in, name); if (name.empty()) { continue; } result = split(name, pattern); cout << result[0] << endl; } }
    转载请注明原文地址: https://ju.6miu.com/read-300152.html

    最新回复(0)