1

I have a string, which looks like this:

foo
$RESULT :(0.2374742, 0.267722, ...up to a million more)
$STATES :{1, 3, 5, ...}
foo 

so somewhere in the string are results and directly after them are the states and I want to save the Results in a list and the states in another list.

I think I need something like "read from $RESULT :(" to ")" get every number and push to list, same for States, but I dont know how to read a String from "a" to "b" and tokenize its content.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
MindlessMaik
  • 271
  • 2
  • 5
  • 16

6 Answers6

2

you could use boost tokenizer: it's a header only library, handy to use

CapelliC
  • 59,646
  • 5
  • 47
  • 90
2
int index = s.find("RESULT: (");
int index2 = s.find("$STATE");

int length = index2 - index;

if (index != string::npos) {
    temp = s.substr(index + 7, length - 8);
}
typedef tokenizer<char_separator<char> > tokenizer;
char_separator<char> sep(",() ");
tokenizer tokens(temp, sep);
for (tokenizer::iterator tok_iter = tokens.begin();
        tok_iter != tokens.end(); ++tok_iter) {
    basic_string<char> tempValue = *tok_iter;

    values.push_back(tempValue);

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
MindlessMaik
  • 271
  • 2
  • 5
  • 16
1

Tokenization in C++ is often done with getline, used so: getline(input stream, string where to save it, seperator character);

Try building a class for reading, that saves every line to a collection, then tokenize each line as needed and send to needed collections in an algorithm.

1

You can use strtok() library function - http://www.cplusplus.com/reference/clibrary/cstring/strtok.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Roopesh Kohad
  • 97
  • 1
  • 4
0

Find the first acurance of the '(' and then the first of ')' sign and get the substring between the two indexes (first is the start and the length is end - start) and then you can do the same for the substring after the first ')' sign (for the states).

temp_str = input_str

do twice {
    start    = findChar(temp_str, '(');
    end      = findChar(temp_str, ')')
    len      = end - start + 1
    result   = substr(temp_str, start, len);  

    save_result_to_file(result)

    temp_str = substr(temp_str, end + 1);
}

Don't remember the exact c++ commands but you will have them for sure.

Odinn
  • 808
  • 5
  • 23
0
#include <string>
#include <vector>
using namespace std;

int main()
{
  //This is your source string
  string Source = "foo $RESULT :(0.2374742, 0.267722) $STATES :{1, 3, 5} fo0";
  //Get the $RESULT section of the string, encapsulated by ( )
  string Results = Source .substr(Source .find("(") + 1, (Source .find(")") - Source .find("(")) - 1);

  //Get the $STATES section of the string, encapsulated by { }
  string States = Source .substr(Source .find("{") + 1, (Source .find("}") - Source .find("{")) - 1);

  vector<double> ResultsList;
  vector<int> StatesList;

  //While the Results string still has remaining ", " token/seperators in it
  while(Results.find(", ") != string::npos)  
  {
    //Get the next value and insert it into the vector (converting it from string to float using atof)
    ResultsList.push_back(atof(Results.substr(0, Results.find(", ")).c_str()));
    //Crop that off the oringal string
    Results = Results.substr(Results.find(", ") + 2);  
  }
  //Push the final value (no remaning tokens) onto the store
  ResultsList.push_back(atof(Results.c_str()));

  //Exactly the same operation with states, just using atoi to convert instead
  while(States .find(", ") != string::npos)  
  {  
    StatesList.push_back(atoi(States.substr(0, States .find(", ")).c_str()));  
    States = States.substr(States.find(", ") + 2);  
  }  
  StatesList.push_back(atoi(States.c_str()));
  return 0;
}
TVOHM
  • 2,740
  • 1
  • 19
  • 29
  • appreciate your Solution, unfortunately checking for "(" or ")" is not sufficient, because they are contained in foo. Only RESULT and STATE are unique in the string. So you have to search from result to state and from state to first apperiance of "}". – MindlessMaik Mar 01 '12 at 15:58