Nov 30, 2009 at 8:29pm UTC
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
Nov 30, 2009 at 8:40pm UTC
You don't increment the iterator.
Nov 30, 2009 at 8:48pm UTC
thanks. it is a typo. even adding it ++, codes will not compile on string(*it).
Nov 30, 2009 at 8:58pm UTC
*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