当尝试用星号替换数组中的字符串时,为什么会出现错误?
Why is error occurs when trying to replace strings in an array with stars?
它不让我替换数组中的字符串,我不知道为什么。
if (t1Array[n] == banArray[o])
{
//t1Array[n] = "***";
t1Array[n].replace(1, 2, 3, "***");
banArrayCount[o] ++;
t1filterfile << t1Array[n];
}
您似乎想用3个星号替换字符串t1Array[n]
的位置1开始的2个字符。
如果是,那么调用将看起来像
t1Array[n].replace( 1, 2, 3, '*' );
这个调用对应于下面的成员函数声明
basic_string& replace(size_type pos, size_type n1, size_type n2, charT c);
或者可以使用以下成员函数
basic_string& replace(size_type pos, size_type n1, const charT* s);
在这种情况下,调用将看起来像
t1Array[n].replace( 1, 2, "***" );
考虑位置从0
开始
我想你误解了std::string::replace
的工作方式:
#include <iostream>
#include <string>
int main() {
std::string str = "0123456789";
std::cout << str.replace(2,3,"***");
}
输出01***56789
。被替换的字符数量不需要匹配字符串的长度:
std::cout << str.replace(2,5,"*");
01*789
替换函数具有以下原型-
replace(starting location of replacement,number of characters to be replaced,a pointer to the string)
相关文章:
- 我的字符计数代码计算错误.为什么
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 为什么加载SDF会导致Mobilizer创建闭环错误
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 为什么我在使用void函数时得到错误代码C2276
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 为什么g++在未执行的代码处标记强制转换错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 为什么错误"permission denied","id returned 1 exit status"仅在 IM 使用 C++ 中的头文件 fstream 时才出现
- 为什么错误:"locate_zone"不是"std::chrono"的成员
- 为什么错误 C6386 缓冲区溢出与 strsafe.h 字符串 Cch 函数
- 为什么错误1400无效的窗口句柄
- 为什么错误LNK2001:在这种情况下未解析的外部符号
- c++为什么错误:no match for 'operator='
- 为什么错误"invalid conversion from 'char' to 'const char*"?