C++ - string.replace 不起作用

标签 c++ string visual-studio-2010 replace

我正在查看一些示例,但我似乎无法理解为什么字符串替换功能不起作用。我使用的是 Visual C++ 2010

我尝试编译的代码行是:

string MyClass::replacestr (const string &input){

    string subString = "str";
    string subString2 = "STR";

    for(int index = input.find(subString);    index != string::npos;  index = input.find(subString, index +subString.length()))
    {
        input.replace(index, 2, subString2);
    }   

}

它在 Visual Studio 中给了我这个错误:

3   IntelliSense: no instance of overloaded function "std::basic_string<_Elem, _Traits, _Ax>::replace [with _Elem=char, _Traits=std::char_traits<char>, _Ax=std::allocator<char>]" matches the argument list and object (the object has type qualifiers that prevent a match)   c:\..test.cpp   36  Test

我只是不明白为什么它不能按照 C++ 引用网站中解释的方式工作。

最佳答案

字符串替换更改字符串的内容。您的字符串被标记为 const。这意味着您无法对其调用替换。

关于C++ - string.replace 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392542/

相关文章:

c++ - 我希望在对右值的引用上调用基于右值的函数......如果有这样的事情

c++ - 字节数组到 Variant

c++ - OpenCV的运行时库和静态库

c++ - 如何将字符串转换为 LPTSTR

multithreading - Perl 多线程文件/列表比较

linq-to-sql - 命名空间 'Linq' 中不存在类型或命名空间名称 'System.Data' 等

c++ - 获取合成时间 C++

c - 将文件中的字符串存储到数组中

java - 如何检查字符串是否仅包含Java中的数字

sql-server - 用于 VS2010 的 SQL Server 管理工作室