简体   繁体   English

从 C++ 中的字符串中删除双引号

[英]remove double quotes from a string in c++

I am stripping off double quotes from a string, but I keep getting this error from the following function.我正在从字符串中去除双引号,但我不断从以下函数中收到此错误。 What is the problem here?这里有什么问题?

void readCSVCell(stringstream& lineStream, string& s) {
    std::getline(lineStream,s,',');
    s.erase(remove( s.begin(), s.end(), '\"' ), s.end());
}

[ERROR] [错误]

c.cpp: In function void readCSVCell(std::stringstream&, std::string&) : c.cpp:在函数void readCSVCell(std::stringstream&, std::string&)
c.cpp:11: error: cannot convert __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > to const char* for argument 1 to int remove(const char*) 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*)

Don't you want something like:你不想要这样的东西:

s.erase(remove( s.begin(), s.end(), '\"' ),s.end());

As remove returns "A forward iterator pointing to the new end of the sequence, which now includes all the elements with a value other than value" rather than removing the values.因为remove返回“一个指向序列新末尾的前向迭代器,它现在包括所有值不是 value 的元素”,而不是删除这些值。

It compiles fine for me though (with gcc 4.4), so perhaps you just need to include <algorithm> and make sure you are either using namespace std or qualify the name.虽然它对我来说编译得很好(使用 gcc 4.4),所以也许您只需要包含<algorithm>并确保您using namespace std或限定名称。

Do you have stdio.h included?你有stdio.h吗? Then there could be a conflict with remove .那么可能会与remove发生冲突。 This is the reason why you always should prefix std -calls with, well, std:: .这就是为什么你总是应该在std -calls 前面加上std::

使用std::removeremove

remove is algorithm, hence you need to do #include <algorithm> . remove是算法,因此您需要执行#include <algorithm> Then while using you should use it as std::remove(...) .然后在使用时你应该将它用作std::remove(...)

remove requires the algorithm header and is from std namespace remove需要algorithm头并且来自std命名空间

I do find the C++ Reference very helpful for quickly getting usage examples and what headers are required.我确实发现C++ 参考对于快速获取使用示例和所需的头文件非常有帮助。 It may not have complete information for some things but it helps as a good start if I am not sure about how to use some parts of C Library, stream Library, strings library, STL Containers or STL Algorithms它可能没有关于某些事情的完整信息,但如果我不确定如何使用 C 库、流库、字符串库、STL 容器或 STL 算法的某些部分,它有助于作为一个好的开始

You can use below code to remove double quotes from string in C++.您可以使用以下代码从 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