Error while inserting char through string :: insert
My question is similar to this one , but there is no sample code there and I didn't find it useful.
My mistake
calling the private constructor of the class 'std :: __ 1 :: __ wrap_iter'
Below is a minimal example of my problem:
#include <iostream>
#include <string>
using namespace std;
int main(){
string g = "this_word";
cout << g << endl;
char temp = g[0];
g.erase(0,1);
cout << g << endl;
g.insert(0,temp); // Compiler seems to dislike this. Why?
cout << g << endl;
return 0;
}
I tried this across two compilers and the same error. Read as far as I could from the standard documentation but don't understand my mistake.
+3
source to share
1 answer
Better to check all the signatures of the std :: string :: insert () overloads , and then decide to use which one. g.insert(0,temp);
just doesn't match any of them.
For insertion, char
you can pass an iterator like
g.insert(g.begin(), temp);
or pass the index and count together:
g.insert(0, 1, temp);
+5
source to share