为什么“std::getline”可以用分隔符分割字符串?

时间:2021-05-04 01:10:24

标签: c++ stringstream

string str("1,2,#,#,3,4,#,#,5,#,#");
stringstream s(str);
while (getline(s, str, ',')) {
    cout << str <<" ";
}
cout << endl;

输出为:

1 2 # # 3 4 # # 5 # #

在第一次调用 getline(s,str,',') 时,我们得到 str = "1"。在第二次调用 getline(s,str,',') 时,为什么我们得到 str = "2" 而不是 "1"

是否有指针定位stringstream的当前位置,每次调用后指针向前移动?

template< class CharT, class Traits, class Allocator >
 
std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,
                                            std::basic_string<CharT,Traits,Allocator>& str,

                                           CharT delim );

我阅读了cppreference,但除了函数原型之外我没有得到任何有用的信息。

0 个答案:

没有答案