【问题标题】:C++ string split error (the complex way)C++字符串拆分错误(复杂方式)
【发布时间】:2013-12-07 22:46:29
【问题描述】:

我正在尝试用以下方式在 C++ 中吐出一个字符串:

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

using namespace std;
size_t const N = 10000000;

typedef string::const_iterator iter;
typedef boost::iterator_range<iter> string_view;

template<typename C>
void test_custom(string const& s, char const* d, C& ret)
{
    C output;

    bitset<255> delims;
    while (*d)
    {
        unsigned char code = *d++;
        delims[code] = true;
    }
    typedef string::const_iterator iter;
    iter beg;
    bool in_token = false;

    bool go = false;

    for (string::const_iterator it = s.begin(), end = s.end(); it != end; ++it)
    {
        if (delims[*it])
        {
            if (in_token)
            {

                output.push_back(typename C::value_type(beg, it));
                in_token = false;
            }
        }
        else if (!in_token)
        {
            beg = it;
            in_token = true;
        }
        else
        {
            if (!go)
            {
                cout << typename C::value_type(beg, it);
                //outputs the first character
                go = true;
            }
        }
    }

    if (in_token)
        output.push_back(typename C::value_type(beg, s.end()));
    output.swap(ret);
}

vector<string_view> split_string(string in, const char* delim = " ")
{
    vector<string_view> vsv;
    test_custom(in, delim, vsv);

    return vsv;
}

int split()
{
    string text = "123 456";

    vector<string_view> vsv = split_string(text);

    for (int i = 0; i < vsv.size(); i++)
        cout << endl << vsv.at(i) << "|" << endl;

    return 0;
}

这里的问题是第一个字符被删除是有原因的......返回的字符串是 ' 23' 和 '456' 但我希望它们是 '123 ' 和 '456'

所以,第一个字符是''而不是'1'

【问题讨论】:

  • 我尽了最大努力,但我无法让它工作......我的代码中有一个cout;第一个字符在typename C::value_type(beg, it),但我不能把它放在output
  • 第一个字符是''而不是'1'

标签: c++ string visual-c++ boost split


【解决方案1】:

我不熟悉boost::iterator_range,但它确实听起来像一对迭代器。

如果是这样,那么在这段代码中:

vector<string_view> split_string(string in, const char* delim = " ")
{
    vector<string_view> vsv;
    test_custom(in, delim, vsv);

    return vsv;
}

您返回的迭代器引用了一个名为 in 的本地 string,该迭代器在函数返回时已不复存在。

这是未定义的行为。

一种解决方法是通过引用传递该字符串。


顺便说一句,在空格上拆分字符串的一种低效但简单且安全的方法是使用istringstream

istringstream stream( source_string );
string word;
while( stream >> word ) { cout << word; }

免责声明:编译器未修改的代码。

【讨论】:

    【解决方案2】:

    既然你已经在使用 Boost,你可以使用这个(“简单”的方式:P):

    #include <boost/algorithm/string.hpp>
    
    std::string text = "this is sample string";
    std::vector<std::string> tokens;
    boost::split(tokens, text, boost::is_any_of("\t "));
    

    或者使用您想用作第三个参数的任何分隔符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多