std::getline (string) & std::stringstream::str

xiaoxiao2021-02-28  120

std::getline (string)

#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<sstream> using namespace std; const int MAXN=105; char s1[MAXN],s2[MAXN],s3[MAXN],s4[MAXN]; int main() { string s; getline(cin,s); stringstream ss(s); string t; while(getline(ss,t,'.')) { cout<<t<<endl; } //for(string s1;ss>>s1;cout<<s1<<endl); return 0; }

(1) istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); (2) istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str);

Get line from stream into string 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.

std::stringstream::str

string str() const; void str (const string& s);

Get/set content The first form (1) returns a string object with a copy of the current contents of the stream.

The second form (2) sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence.

Internally, the function calls the str member of its internal string buffer object.

Example

// stringstream::str #include <string> // std::string #include <iostream> // std::cout #include <sstream> // std::stringstream, std::stringbuf int main () { std::stringstream ss; ss.str ("Example string"); std::string s = ss.str(); std::cout << s << '\n'; return 0; }
转载请注明原文地址: https://www.6miu.com/read-46088.html

最新回复(0)