0

I have a function MakeElementfromString( string k ){} i want to split the string and make struct element{int nr, string s} with it . what can i use do do that ? found strtok but couldn't use it or maybe i dont know how because is for char and some stringstream method.. nothing worked for me anyone can tell me an idea ? i am not an expert in c++ so explain kind :) thank you

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 1
    If you are using C++ strings use substr() , rather than strtok. – DumbCoder May 09 '11 at 12:08
  • 3
    How do you want the string to be converted into a struct? What have you tried so far? What worked and what did'nt? And is this a homework question? – Sriram May 09 '11 at 12:09
  • What does the variable "nr" represent? – jonsca May 09 '11 at 12:13
  • possible duplicate of [C++: How to split a string?](http://stackoverflow.com/questions/236129/c-how-to-split-a-string) – Bo Persson May 09 '11 at 12:16
  • @user745087: I have tried something based on limited information. See if that serves the purpose? – Sriram May 09 '11 at 12:16
  • @Bo: I am not sure if this is a duplicate of that question you indicate. O.P is also asking about stringstream, strtok and related functions. It is a duplicate only in the broadest sense of the term. – Sriram May 09 '11 at 12:17

3 Answers3

1

If you can use libs use boost::split.

If you can't iterate over your string and put the parts in a vector.

string s("test hallo! someothertest");
char separator = ' ';
vector<string> parts;
int token_begin = 0;
for(int i = 0; i < s.size(); ++i){
  if( s[i] == separator){
     parts.push_back(s.substr(token_begin, i - token_begin ));
     token_begin = i + 1;
  }
}

//get last token if does not end with a separator
if(token_begin != s.size()){
  parts.push_back(s.substr(token_begin, s.size() - token_begin));
}
RedX
  • 14,749
  • 1
  • 53
  • 76
0

At the time of this writing, the question has not been edited to say how the OP wants the string to be converted to a struct element. But this is the broad outline of how it might be done.

Assumptions: string s has first two characters that go into int member of struct and the rest of it goes into the string part.

example: s = 01hello
Pseudo-code:

string num = s.substr(0, 2);  
string rest = s.substr(3);  

element e;
e.setVal(num, rest);

setVal(string n, string m) {
  str = m;
  istringstream buffer(n);
  buffer >> num;
}

where original struct is:

struct element {
 int nr;
 string str;
};  
Sriram
  • 10,298
  • 21
  • 83
  • 136
0

thank you for help made it for

struct Telem {
int nrte;
string s;
int dims;
};

and i needed to read from file a line and the line to convert into my desire element hope it helps some other people for similar problems

Telem TelemDinString( string k )
{
    Telem a;
    Init(a);

    string buf;
    stringstream ss(k);
    vector<string> tokens;
    while ( ss >> buf )
        tokens.push_back(buf);
    int nr;

    stringstream convert( tokens[0] );
    if ( !( convert >> nr ) )
        nr=-1;
    a.nrte = nr;
    a.s = tokens[1];
    a.dims=a.s.length();

return a;
}