0

在 C++ 中,假设我从流中提取行,模仿getline(). 每次我提取一行时,例如使用低级原语,我还想创建一个string_view由提取的字符串支持的 s 向量。因此,本质上,我的方法将返回一个提取的字符串和一个string_view由前者支持的 s 向量。如何做到这一点?遵循相关的代码片段:

bool Splitter( istream &is, string &backbone, vector<string_view> &words ) {
    int ch;
    words.clear(), backbone.clear();
    for ( ;(ch= is.get()) != EOF and ch != '\n'; backbone.push_back(ch) ) ;
    int i= 0, j, k= backbone.size();
#define skip_space(i) {for(;i < k and isspace(backbone.at(i)); ++i);}
    skip_space(i);
    assert( i == k or not isspace(backbone.at(i)) );
    for (;i < k; i= j ) {
        for ( j= i+1; j < k and not isspace(backbone.at(j)); ++j ) ;
        assert( j-i > 0 );
        words.emplace_back(backbone.substr(i,j==k?string::npos:j-i)); // <-- how to avoid creating a new string?
        skip_space(j);
    }
#if DBG
    cout << backbone << endl;
    for ( i= 0; i < words.size(); ++i )
        cout << words[i] << ", ";
    cout << endl;
#endif
    return not(ch == EOF and backbone.empty() and words.empty());
}
4

1 回答 1

0

您可以包装backboneastring_view因为它substr()返回 a string_view(它也方便地公开at()),例如:

    ...
    string_view backbone_view(backbone);
    skip_space(i);
    assert( i == k or not isspace(backbone_view.at(i)) );
    for (;i < k; i= j ) {
        for ( j= i+1; j < k and not isspace(backbone_view.at(j)); ++j ) ;
        assert( j-i > 0 );
        words.emplace_back(backbone_view.substr(i,j==k?string::npos:j-i));
        skip_space(j);
    }
    ...
于 2019-10-20T09:55:04.317 回答