C++で複数行のデータを読み込んでひとつのベクトルに格納する
改行を含むコンマ区切りのデータを一つのvectorに格納する場合。
fileの内容は以下の通りでintが6つである。
1,10,100, 2,20,200,
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; class InputManager { private: public: void Get_TxtData(string filename) { ifstream ifs(filename); if (ifs.fail()) cout << "ifs is failed." << endl; string str; vector<int> container; while (getline(ifs, str, ',')){ if (str[0] == '\n') str.erase(str.begin(), str.begin() + 1); container.push_back(stoi(str)); } return; } };
コンマで区切られた各数字がcontainerに数値として格納される。