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

6
  • 1
    If you are using C++ strings use substr() , rather than strtok. Commented May 9, 2011 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? Commented May 9, 2011 at 12:09
  • What does the variable "nr" represent? Commented May 9, 2011 at 12:13
  • possible duplicate of C++: How to split a string? Commented May 9, 2011 at 12:16
  • @user745087: I have tried something based on limited information. See if that serves the purpose? Commented May 9, 2011 at 12:16

3 Answers 3

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));
}
Sign up to request clarification or add additional context in comments.

Comments

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;
};  

1 Comment

@user745087: if it solved your problem, you should upvote and mark the correct answer.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.