Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串迭代器不兼容,无法读取每个字符串_C++_String_Boost_Assert_Boost Tokenizer - Fatal编程技术网

C++ 字符串迭代器不兼容,无法读取每个字符串

C++ 字符串迭代器不兼容,无法读取每个字符串,c++,string,boost,assert,boost-tokenizer,C++,String,Boost,Assert,Boost Tokenizer,我得了性病。 我想对std::ostringstream的每一行进行迭代 我使用boost::tokenizer: std::ostringstream HtmlStream; ............. typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer; line_tokenizer tok(HtmlStream.str(), boost::char_separator<ch

我得了性病。 我想对std::ostringstream的每一行进行迭代

我使用boost::tokenizer:

std::ostringstream HtmlStream;
.............
typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer;
line_tokenizer tok(HtmlStream.str(), boost::char_separator<char>("\n\r"));

for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end(); i != end; ++i)
{
    std::string str = *i;
}
我的断言错误为“字符串迭代器不兼容”。 我已经在谷歌和StackOverflow上读到了这个错误,但是我很难找到我的错误

有人能帮我吗

非常感谢

致以最良好的祝愿


Nixeus

我喜欢将其设置为非复制,以实现效率/错误报告:

查看它

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <iostream>
#include <vector>

int main()
{
    auto const& s = "hello\r\nworld";

    std::vector<boost::iterator_range<char const*>> lines;
    boost::split(lines, s, boost::is_any_of("\r\n"), boost::token_compress_on);

    for (auto const& range : lines)
    {
        std::cout << "at " << (range.begin() - s) << ": '" << range  << "'\n";
    };
}
#include <boost/spirit/include/qi.hpp>

int main()
{
    std::string const s = "hello\r\nworld";

    std::vector<std::string> lines;

    {
        using namespace boost::spirit::qi;
        auto f(std::begin(s)), 
             l(std::end(s));
        bool ok = parse(f, l, *(char_-eol) % eol, lines);
    }

    for (auto const& range : lines)
    {
        std::cout << "'" << range  << "'\n";
    };
}
这比所示的大多数备选方案更有效。当然,如果您需要更多的解析能力,请考虑BoooSoviv:

查看它

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <iostream>
#include <vector>

int main()
{
    auto const& s = "hello\r\nworld";

    std::vector<boost::iterator_range<char const*>> lines;
    boost::split(lines, s, boost::is_any_of("\r\n"), boost::token_compress_on);

    for (auto const& range : lines)
    {
        std::cout << "at " << (range.begin() - s) << ": '" << range  << "'\n";
    };
}
#include <boost/spirit/include/qi.hpp>

int main()
{
    std::string const s = "hello\r\nworld";

    std::vector<std::string> lines;

    {
        using namespace boost::spirit::qi;
        auto f(std::begin(s)), 
             l(std::end(s));
        bool ok = parse(f, l, *(char_-eol) % eol, lines);
    }

    for (auto const& range : lines)
    {
        std::cout << "'" << range  << "'\n";
    };
}
#包括
int main()
{
std::string const s=“hello\r\nworld”;
std::矢量线;
{
使用名称空间boost::spirit::qi;
自动f(标准::开始),
l(标准:结束);;
bool ok=parse(f,l,*(char_u-eol)%eol,行);
}
用于(自动常数和范围:行)
{

std::cout既然您似乎不介意复制,那么将
str()
复制到
istringstream
中并使用
std::getline()
?我对boost是新手::“世界”所以我需要学习它!你能给我一个示例代码吗?使用iStringStream?Thx:)@WalterFabioSimoni:我想Chad的意思是创建一个实例
std::iStringStream
用字符串初始化,然后使用
std::getline()
像处理文件流一样使用它。这将避免完全使用Boost。它可以为您工作,但它不能完成
Boost::tokenizer
所能做的一切(例如忽略连续分隔符字符).
警告:变量“ok”未使用
@LightnessRacesinOrbit您将知道您需要使用它做什么:)或不做什么。请注意,第二个示例的介绍内容为:“如果您需要更多解析功能”。这意味着你99%的时间都需要输入验证。至少可以让它成为
const
;)我不想让它感觉被使用或受到任何限制。让变量开花,不管它的寿命有多小。我的宗教信仰让我不去压碎最小的生命。
const
促进了生命的自由。传播生命的自由露丝是生命的象征,而不是意外突变带来的死亡之痛!(阅读:癌症)