通过字符串::插入插入字符时出错



我的问题与此类似,但那里没有示例代码,而且我觉得它没有帮助。

我的错误是

调用类的私有构造函数 'std::__1::__wrap_iter'

我的问题的最小示例可以在下面找到:

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

我已经通过两个编译器和相同的错误尝试了这个。 尽可能多地阅读标准文档,但不了解我的错误。

最好检查 std::string::insert(( 重载的所有签名,然后决定使用哪一个。g.insert(0,temp);只是与它们中的任何一个都不匹配。

对于插入char,您可以传递一个迭代器,例如

g.insert(g.begin(), temp);

或者传递索引并一起计数:

g.insert(0, 1, temp);

最新更新