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

发布于 2024-11-01 11:24:40 字数 661 浏览 1 评论 0 原文

我正在从字符串中去掉双引号,但我不断从以下函数中收到此错误。这里有什么问题呢?

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* 对于参数 1int remove(const char*)

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: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*)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岁月静好 2024-11-08 11:24:40

难道你不想要这样的东西:

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

remove 返回“指向序列新末尾的前向迭代器,它现在包括具有除 value 之外的值的所有元素”而不是删除值。

不过它对我来说编译得很好(使用 gcc 4.4),所以也许你只需要包含 并确保你是 using namespace std 或限定名称。

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.

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.

云醉月微眠 2024-11-08 11:24:40

你有包含stdio.h吗?那么可能会与remove发生冲突。这就是为什么你总是应该在 std-calls 前面加上 std:: 前缀。

Do you have stdio.h included? Then there could be a conflict with remove. This is the reason why you always should prefix std-calls with, well, std::.

潇烟暮雨 2024-11-08 11:24:40

使用 std::remove 而不是 remove

Use std::remove not remove

隔岸观火 2024-11-08 11:24:40

您可以使用下面的代码从 C++ 中的字符串中删除双引号。

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

You can use below code to remove double quotes from string in C++.

stringVariable.erase(
    std::remove(stringVariable.begin(), stringVariable.end(), '\"'), 
    stringVariable.end());
苍白女子 2024-11-08 11:24:40

remove 是算法,因此您需要执行#include 。然后在使用时应将其用作 std::remove(...)

remove is algorithm, hence you need to do #include <algorithm>. Then while using you should use it as std::remove(...).

半城柳色半声笛 2024-11-08 11:24:40

remove 需要 algorithm 标头并且来自 std 命名空间

我确实发现 C++ 参考 对于快速获取使用示例和标题非常有帮助是必需的。它可能没有某些内容的完整信息,但如果我不确定如何使用 C 库、流库、字符串库、STL 容器或 STL 算法的某些部分,它可以作为一个良好的开始。

remove requires the algorithm header and is from std namespace

I 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

晌融 2024-11-08 11:24:40

我迟到了,但这里有一种适用于 C++14 及以上版本的方法 std::quoted。这很方便,因为 CSV 也可能有自定义转义字符。

简而言之,它将 csv 双双引号转义字符串 "something ""include escapedquotes""cool" 转换为 something "include escapedquotes"cool

现场演示

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>

int main()
{
    const char delim{'"'};
    const char escape{'"'};
    constexpr static std::string_view TEXT ="\"something \"\"including escaped quotes\"\" cool\"";

    std::stringstream ss;
    ss << TEXT;
    std::cout << "stored as [ "<< ss.str() << "]\n";
    std::string out;
    ss >> std::quoted(out,delim, escape);
    std::cout << "written out ["<< out << "]\n";
    return 0;
}

输出:

stored as ["something ""including escaped quotes"" cool"]
written out [something "including escaped quotes" 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" into something "including escaped quotes" cool

Live Demo

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>

int main()
{
    const char delim{'"'};
    const char escape{'"'};
    constexpr static std::string_view TEXT ="\"something \"\"including escaped quotes\"\" cool\"";

    std::stringstream ss;
    ss << TEXT;
    std::cout << "stored as [ "<< ss.str() << "]\n";
    std::string out;
    ss >> std::quoted(out,delim, escape);
    std::cout << "written out ["<< out << "]\n";
    return 0;
}

Output:

stored as ["something ""including escaped quotes"" cool"]
written out [something "including escaped quotes" cool]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文