簡體   English   中英

從 C++ 中的字符串中刪除雙引號

[英]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*為參數1int 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::removeremove

remove是算法,因此您需要執行#include <algorithm> 然后在使用時你應該將它用作std::remove(...)

remove需要algorithm頭並且來自std命名空間

我確實發現C++ 參考對於快速獲取使用示例和所需的頭文件非常有幫助。 它可能沒有關於某些事情的完整信息,但如果我不確定如何使用 C 庫、流庫、字符串庫、STL 容器或 STL 算法的某些部分,它有助於作為一個好的開始

您可以使用以下代碼從 C++ 中的字符串中刪除雙引號。

stringVariable.erase(
    std::remove(stringVariable.begin(), stringVariable.end(), '\"'), 
    stringVariable.end());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM