Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of C++ stringstream formatted output and input

Shulou Source: shulou.com Published: 2022-06-02 15:44:02 09月26日 Update

This article mainly explains the "C++ stringstream formatting output input analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C++ stringstream formatting output input analysis" bar!

Catalogue

Recently, I often encounter all kinds of input problems in the written test, so I sum up a wave carefully; first, string str; cin > > str; encounters the end of a space; then getline () is generated, which can be used to get a line of string; the space is removed automatically, as long as it doesn't mix cin and getline.

Cin.getline (Smallork)

Receive k characters in a line, you can receive spaces

Cin.getline () actually takes three parameters, cin.getline (string, number received, end character)

When the third parameter is omitted, the system defaults to'\ 0'

Getline (cin,s)

Similar to cin.getline (), reading a line of strings, it is worth noting that cin.getline () belongs to the istream stream, while getline () belongs to the string stream, which is not the same.

StringStream

This is a separate, important thing that is included in the sstream library.

The istringstream class is used to perform C++-style streaming input operations.

The ostringstream class is used to perform C-style streaming output operations.

The stringstream class can also support C-style streaming input and output operations.

The purpose of stringstream is then to read characters or strings from the string object.

Strings = "ABCD"; stringstream ss (s); char ch; while (ss > > ch) {cout str) {cout x) {sum + = x;} cout > id; vector vec; / / for (int I = 0; I

< N; i++) //{ // vector temp; // int user; // getline(cin, str); // stringstream s(str); // while (s>

> user) / / divide by spaces / / {/ / temp.push_back (user); / /} / / vec.push_back (temp); / /} while (getline (cin, str)) / / you may not know the number of lines {vector temp; int user; stringstream s (str) While (s > > user) {temp.push_back (user);} vec.push_back (temp);} cout N > > id; / / directly enter the number of users and the user id; to be found so that the newline character int N; vector in; char c is generated While ((c = cin.get ())! ='\ n') {cin.unget (); cin > > N; in.push_back (N);} vector vec; for (int I = 0; I

< in[0]; i++) { vector temp; int user; while ((c=cin.get())!= '\n') //文件结果没有换行符了,所以陷入死循环 { cin.unget(); cin >

> user; temp.push_back (user);} if (temp.size ()! = 0) {vec.push_back (temp);}} cout

Tags: Input character string space output newline one line processing clocking analysis result format function parameter matrix style learning three code content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Shulou Technology Microsoft vpn