[英]remove double quotes from a string in c++
我正在從字符串中去除雙引號,但我不斷從以下函數中收到此錯誤。 這里有什么問題?
void readCSVCell(stringstream& lineStream, string& s) {
std::getline(lineStream,s,',');
s.erase(remove( s.begin(), s.end(), '\"' ), s.end());
}
[錯誤]
c.cpp:在函數
void readCSVCell(std::stringstream&, std::string&)
:
c.cpp:11:錯誤:不能轉換__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >
到const char*
為參數1
至int remove(const char*)
你不想要這樣的東西:
s.erase(remove( s.begin(), s.end(), '\"' ),s.end());
因為remove
返回“一個指向序列新末尾的前向迭代器,它現在包括所有值不是 value 的元素”,而不是刪除這些值。
雖然它對我來說編譯得很好(使用 gcc 4.4),所以也許您只需要包含<algorithm>
並確保您using namespace std
或限定名稱。
你有stdio.h
嗎? 那么可能會與remove
發生沖突。 這就是為什么你總是應該在std
-calls 前面加上std::
。
使用std::remove
不remove
remove
是算法,因此您需要執行#include <algorithm>
。 然后在使用時你應該將它用作std::remove(...)
。
您可以使用以下代碼從 C++ 中的字符串中刪除雙引號。
stringVariable.erase(
std::remove(stringVariable.begin(), stringVariable.end(), '\"'),
stringVariable.end());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.