string iteration

trying to iterate a string, and attach each char to other string variables, having problem to compile the following codes

string var1= "sam";
string var2 = ""
string list = "abacd";

string::iterator it = list.begin();
while ( it != list.end())
{
var2 = var1 + string(*it);
}

thanks for the help

chris
You don't increment the iterator.
thanks. it is a typo. even adding it ++, codes will not compile on string(*it).
*it will return a char, strings don't have a constructor which take a single char
This should work: var2 = var1 + *it;
You are also missing a semicolon after var2 declaration
thanks. it seems work.
Topic archived. No new replies allowed.