从 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
到、std::allocator > > const char*
对于参数1
到int remove(const char*)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
难道你不想要这样的东西:
当
remove
返回“指向序列新末尾的前向迭代器,它现在包括具有除 value 之外的值的所有元素”而不是删除值。不过它对我来说编译得很好(使用 gcc 4.4),所以也许你只需要包含
并确保你是using namespace std
或限定名称。Don't you want something like:
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.It compiles fine for me though (with gcc 4.4), so perhaps you just need to include
<algorithm>
and make sure you are eitherusing namespace std
or qualify the name.你有包含
stdio.h
吗?那么可能会与remove
发生冲突。这就是为什么你总是应该在std
-calls 前面加上std::
前缀。Do you have
stdio.h
included? Then there could be a conflict withremove
. This is the reason why you always should prefixstd
-calls with, well,std::
.使用
std::remove
而不是remove
Use
std::remove
notremove
您可以使用下面的代码从 C++ 中的字符串中删除双引号。
You can use below code to remove double quotes from string in C++.
remove
是算法,因此您需要执行#include
。然后在使用时应将其用作std::remove(...)
。remove
is algorithm, hence you need to do#include <algorithm>
. Then while using you should use it asstd::remove(...)
.remove 需要
algorithm
标头并且来自std
命名空间我确实发现 C++ 参考 对于快速获取使用示例和标题非常有帮助是必需的。它可能没有某些内容的完整信息,但如果我不确定如何使用 C 库、流库、字符串库、STL 容器或 STL 算法的某些部分,它可以作为一个良好的开始。
remove requires the
algorithm
header and is fromstd
namespaceI do find the C++ Reference very helpful for quickly getting usage examples and what headers are required. 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++14 及以上版本的方法
std::quoted
。这很方便,因为 CSV 也可能有自定义转义字符。简而言之,它将 csv 双双引号转义字符串
"something ""include escapedquotes""cool"
转换为something "include escapedquotes"cool
现场演示
输出:
I'm late to the party but here's a way applies to C++14 and above with
std::quoted
. It's handy since CSV might have custom escape characters as well.In short, it translates csv double-double quote escaped string
"something ""including escaped quotes"" cool"
intosomething "including escaped quotes" cool
Live Demo
Output: