刷题或者在线编程考试的时候需要对输入的字符串进行分割,封装后再进行处理;
这里给出了一个通用的C++风格的字符串分割函数,适用不同的分割符。
函数主体
前提说明: 输入的字符串string中包含string 或者 int再加分割符。
stringstream类
<sstream>
库定义了三种类:istringstream、ostringstream和stringstream
,分别用来进行流的输入、输出和输入输出操作。
stringstream::str(); returns a string object with a copy of the current contents of the stream.
stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.
stringstream清空,stringstream s; s.str("");
- 实现任意类型的转换的模板
|
|
getline()函数
|
|
官方解释:
extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’, for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Note that any content in str before the call is replaced by the newly extracted sequence.
Each extracted character is appended to the string as if its member push_back was called.
大意为:
对于(1),输入流is按分割符delim分割 对于(2),输入流is遇到回车换行(’\n’)分割);
分割段转换为string存入str,下一次调用,str的内容会被下一个分割段替换。