[英]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> > >
toconst char*
for argument1
toint remove(const char*)
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*)
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
或限定名称。
使用std::remove
不remove
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.