I've run into a problem when I'm trying to insert an int into a string. I'm using the string::insert()-method for this which only accepts a char argument. All the methods which I have used, itoa, stringstreams and so on, only seem to return a char-pointer, which does not work out.
So what I want to do is pretty much
mystring.insert(mystring.begin(), 1, myint(as a char));
Integers are often more than one character, so you can't pass one to insert into a string. You could use atoi to get a C-string, then insert each character, I suppose.
You can do an explicit cast: mystring.insert(mystring.begin(), 1, (char)myint));. But an integer will implicity cast into a char so I cant really see an issue. Rembmer than an int is always bigger than a char so the value of your int may not fit into the char properly.
That will work, but it won't do what OP intended. Casting to a char will give you the character at that ASCII value, not insert the numbers as characters.
What if he wanted to insert at a certain pos in string.
Another example but thise use size_t pos not iterators:
1 2 3 4 5 6 7
std::string str = ", hello";
int h = 12122;
char h_string[33];// max bits for a integer on 32-bit
//;
sprintf(h_string,"%d",h);// or _itoa(h,h_string,10)
//inserting
str.insert(0,std::string(h_string));