0

I take the input from text file. Inputs are similar with the example given below.

size 5,

turn_count 3,

entity 1 ACDEF 2x2,
 
entity 2 BDFHC 4x5,
 
entity 3 CDHGF 5x5,
 
turn 1 2x3 4x5 5x4,

turn 2 3x3 4x4 5x3,

turn 3 3x4 4x3 5x2.

I assign this input line by line into a vector<string> by getline(). I want to access ACDEF, BDHGF, CDHGF seperately and assign to another vector. What is the best way to do it?

mucahitrtn
  • 57
  • 1
  • 8
  • 1
    Use something like `std::stringstream` to further split each line into its component words and interpret it. – Botje May 19 '20 at 12:18
  • 1
    Does this answer your question? [Split a string using C++11](https://stackoverflow.com/questions/9435385/split-a-string-using-c11) – cigien May 19 '20 at 13:24

3 Answers3

1

As user31264 suggested you could write a split function and split every line you got into its words.

IMHO this is a better and more flexible split function though:

#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> split(const std::string& str, char delimiter)
{
    std::vector<std::string> tokens;
    std::string token;
    std::istringstream tokenStream(str);
    while(std::getline(tokenStream, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

What that function does is creating a stringstream out of your string (your line) and extracting words delimited by char delimiter by using getline() on that stream.

So while reading your file with getline() you could pass every line into that split function with

auto words = split(line, ' ');

and iterate over all words that line contains.

Stefan Riedel
  • 796
  • 4
  • 15
0

You may use istringstream, or write a simple function:

vector<string> Split(const string& s) {
    vector<string> vsWords;
    string sWord;
    for (char c : s) {
        if (isspace(c)) {
            vsWords.push_back(sWord);
            sWord.clear();
        }
        else
            sWord += c;
    }
    if (!sWord.empty())
        vsWords.push_back(sWord);
    return vsWords;
}
user31264
  • 6,557
  • 3
  • 26
  • 40
-1

With the assumption, text.txt looks like you've given:

size 5,

turn_count 3,

entity 1 ACDEF 2x2,

entity 2 BDFHC 4x5,

entity 3 CDHGF 5x5,

turn 1 2x3 4x5 5x4,

turn 2 3x3 4x4 5x3,

turn 3 3x4 4x3 5x2.

this code snippet should find those token of your interest: ACDEF, BDFHC, CDHGF:

#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>

using namespace std;

int main(){
    string line;
    vector<string> vect;

    // result vector with the found string
    vector<string> vectResult;

    // token you want to find
    // can also use array or whatever you like
    const list<string> lst = {"ACDEF","BDFHC","CDHGF"};

    // get your text.txt into a vector
    ifstream myfile ("../src/text.txt");
    if (myfile.is_open()){
        while ( getline (myfile, line)){
            vect.push_back(line);
        }
        myfile.close();
    }

    // constructor for the token iterator
    regex_token_iterator<string::iterator> rend;

    // for every token of interest
    for (string x : lst){   
        // go through my initial vector
        for(string text : vect){
            // extract it from the line, only if it is founded.
            if (regex_search(text, regex(x))){
                regex re(x);
                regex_token_iterator<string::iterator> a ( text.begin(), text.end(), re);
                while (a!=rend)
                    // put the token of interest into the result vector
                    vectResult.push_back(*a++);
            }
        }
    }
    // check content of your new vector
    for (string x: vectResult)
        cout << x << " ";
    return 0;
}

usually you'd like to change the regex the way it fits your needs.

dboy
  • 1,004
  • 2
  • 16
  • 24