擦除功能是給的參數無效的錯誤,但我已經通過了爲什麼擦除函數在字符串函數中給出錯誤?
第一個參數 =指數無。
第二個參數 = 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
謝謝.....已解決 –