在 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());
}