擦除功能是给的参数无效的错误,但我已经通过了为什么擦除函数在字符串函数中给出错误?
第一个参数 =指数无。
第二个参数 = no。的字符
然后它也给错误。 如
word3 = word2.erase(word.begin(), word.length()/2-1);
擦除功能是给的参数无效的错误,但我已经通过了为什么擦除函数在字符串函数中给出错误?
第一个参数 =指数无。
第二个参数 = no。的字符
然后它也给错误。 如
word3 = word2.erase(word.begin(), word.length()/2-1);
看着http://en.cppreference.com/w/cpp/string/basic_string/erase(1)你看,你需要传递的第一个字符的指数擦除和多少个字符删除此号码。你传递一个迭代器作为第一个参数。只要做到
word3 = word2.erase(0, word.length()/2 -1);
// ^^^^
// this should probably be word2
或使用(3)重载接受范围:
word3 = word2.erase(word2.begin(), std::next(word2.begin(), word2.length()/2 -1));
我也相信你应该有你的word2
erase
,不word
内。
类的成员函数begin()
返回迭代器。这是其返回类型std::string::iterator
或std::string::const_iterator
。
您正在尝试使用成员函数erase
有std::string::size_type
类型的参数:
basic_string& erase(size_type pos = 0, size_type n = npos);
如果你想使用这个成员函数,你应该写这样
word2.erase(0, word.length()/2 - 1)
如果你想呼叫使用使用以下成员函数的迭代器的函数
iterator erase(const_iterator first, const_iterator last);
则呼叫可以像
word2.erase(word.begin(), std::next(word.begin(), word.length()/2 - 1))
或者干脆
word2.erase(word.begin(), word.begin() + word.length()/2 - 1)
我希望这不会是你调用函数对象与名称word2
,并在用作参数表达的错字你使用对象名称word
谢谢.....已解决 –