Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <string>
- constexpr auto str_end = std::string::npos;
- using std::vector;
- using std::string;
- using std::ostream;
- using std::ifstream;
- using std::getline;
- using std::cout;
- using std::endl;
- // stream vector elements out
- template <typename T>
- ostream& operator<<(ostream& os, const vector<T>& vec) {
- for (const auto& elem : vec) os << elem << ' ';
- return os;
- }
- // given a string with delimiters inside, parse it into
- // individual tokens stored in a vector<string>
- void tokenize(const string& str, vector<string>& tokens,
- const string& delimiters = " ") {
- auto last_pos = str.find_first_not_of(delimiters, 0); // first token
- auto curr_pos = str.find_first_of(delimiters, last_pos); // next delim
- while (curr_pos != str_end || last_pos != str_end) {
- tokens.emplace_back(str.substr(last_pos, curr_pos - last_pos));
- last_pos = str.find_first_not_of(delimiters, curr_pos); // next token
- curr_pos = str.find_first_of(delimiters, last_pos); // next delim
- }
- }
- /*
- // general wrapper ?
- // -exclude string T's somehow?
- template <typename T>
- void tokenize(const string& str, vector<T>& tokens,
- const string& delimiters = " ") {
- vector<string> str_tokens;
- tokenize(str, str_tokens, delims);
- for (const auto& e : str_tokens)
- tokens.emplace_back( ??? (e)); // need T's type, then the right conversion
- }
- */
- // int wrapper
- void tokenize(const string& str, vector<int>& tokens,
- const string& delimiters = " ") {
- vector<string> str_tokens;
- tokenize(str, str_tokens, delims);
- for (const auto& e : str_tokens)
- tokens.emplace_back(stoi(e)); // ints
- }
- // double wrapper
- void tokenize(const string& str, vector<double>& tokens,
- const string& delimiters = " ") {
- vector<string> str_tokens;
- tokenize(str, str_tokens, delims);
- for (const auto& e : str_tokens)
- tokens.emplace_back(stod(e)); // doubles
- }
- int main() {
- ifstream fs{"data"};
- string tmp{""};
- const string delims{"[,]"};
- //vector<string> tokens;
- //vector<int> tokens;
- vector<double> tokens;
- while (getline(fs, tmp)) tokenize(tmp, tokens, delims);
- cout << tokens << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement