Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

char c1, c2, c3;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

std::istringstream("a b c")

>>

c1

>> c2 >> c3;

 

 

 

 

 

 

 

std::cout << "Default

behavior:

c1

= " <<

c1

<< "

c2

= " << c2

<< "

c3

= " << c3

<< '\n';

std::istringstream("a b c")

>> std::noskipws >> c1 >> c2 >> c3;

 

 

 

 

std::cout << "noskipws

behavior:

c1

= " <<

c1

<< "

c2

= " << c2

<< "

c3

= " << c3

<< '\n';

// Output: Default behavior:

c1

= a

c2

=

b

c3 = c

 

 

 

 

 

 

// noskipws behavior:

c1 =

a

c2

=

c3

=

b

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Default is std::ios_base::skipws.

std::quoted(s[, delim[, escape]]) [C++14] - inserts or extracts quoted strings with embedded spaces.

s - the string to insert or extract.

delim - the character to use as the delimiter, " by default.

escape - the character to use as the escape character, \ by default.

#include <sstream>

...

std::stringstream ss;

std::string in = "String with spaces, and embedded \"quotes\" too"; std::string out;

ss << std::quoted(in);

 

std::cout <<

"read in

[" << in << "]\n"

<<

"stored as

[" << ss.str() << "]\n";

ss >> std::quoted(out);

 

std::cout <<

"written out [" << out << "]\n";

// Output:

 

 

// read in

[String with spaces, and embedded "quotes" too]

// stored as

["String with spaces, and embedded \"quotes\" too"]

// written out [String with spaces, and embedded "quotes" too]

For more information see the link above.

Section 14.2: Output stream manipulators

std::ends - inserts a null character '\0' to output stream. More formally this manipulator's declaration looks like

template <class charT, class traits>

std::basic_ostream<charT, traits>& ends(std::basic_ostream<charT, traits>& os);

and this manipulator places character by calling os.put(charT()) when used in an expression

os << std::ends;

std::endl and std::flush both flush output stream out by calling out.flush(). It causes immediately producing output. But std::endl inserts end of line '\n' symbol before flushing.

std::cout << "First line." << std::endl << "Second line. " << std::flush << "Still second line.";

GoalKicker.com – C++ Notes for Professionals

73

//Output: First line.

//Second line. Still second line.

std::setfill(c) - changes the fill character to c. Often used with std::setw.

std::cout << "\nDefault fill: " << std::setw(10) << 79 << '\n'

<<"setfill('#'): " << std::setfill('#')

<<std::setw(10) << 42 << '\n';

// Output:

// Default fill: 79 // setfill('#'): ########79

std::put_money(mon[, intl]) [C++11]. In an expression out << std::put_money(mon, intl), converts the monetary value mon (of long double or std::basic_string type) to its character representation as specified by the std::money_put facet of the locale currently imbued in out. Use international currency strings if intl is true, use currency symbols otherwise.

long double money = 123.45;

// or std::string money = "123.45";

std::cout.imbue(std::locale("en_US.utf8"));

std::cout << std::showbase << "en_US: " << std::put_money(money)

<<" or " << std::put_money(money, true) << '\n';

//Output: en_US: $1.23 or USD 1.23

std::cout.imbue(std::locale("ru_RU.utf8")); std::cout << "ru_RU: " << std::put_money(money)

<<" or " << std::put_money(money, true) << '\n';

//Output: ru_RU: 1.23 руб or 1.23 RUB

std::cout.imbue(std::locale("ja_JP.utf8")); std::cout << "ja_JP: " << std::put_money(money)

<<" or " << std::put_money(money, true) << '\n';

//Output: ja_JP: 123 or JPY 123

std::put_time(tmb, fmt) [C++11] - formats and outputs a date/time value to std::tm according to the specified

format fmt.

tmb - pointer to the calendar time structure const std::tm* as obtained from localtime() or gmtime(). fmt - pointer to a null-terminated string const CharT* specifying the format of conversion.

#include <ctime>

...

std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t);

std::cout.imbue(std::locale("ru_RU.utf8"));

std::cout << "\nru_RU: " << std::put_time(&tm, "%c %Z") << '\n';

//Possible output:

//ru_RU: Вт 04 июл 2017 15:08:35 UTC

For more information see the link above.

GoalKicker.com – C++ Notes for Professionals

74

Section 14.3: Input stream manipulators

std::ws - consumes leading whitespaces in input stream. It di erent from std::skipws.

#include <sstream>

...

std::string str;

 

 

 

std::istringstream("

\v\n\r\t

Wow!There

is no whitespaces!") >> std::ws >> str;

std::cout << str;

 

 

 

// Output: Wow!There

is no whitespaces!

 

std::get_money(mon[, intl]) [C++11]. In an expression in >> std::get_money(mon, intl) parses the character input as a monetary value, as specified by the std::money_get facet of the locale currently imbued in in, and stores the value in mon (of long double or std::basic_string type). Manipulator expects required international currency strings if intl is true, expects optional currency symbols otherwise.

#include <sstream> #include <locale>

...

std::istringstream in("$1,234.56 2.22 USD 3.33"); long double v1, v2;

std::string v3;

in.imbue(std::locale("en_US.UTF-8"));

in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true); if (in) {

std::cout << std::quoted(in.str()) << " parsed as: " << v1 << ", " << v2 << ", " << v3 << '\n';

}

//Output:

//"$1,234.56 2.22 USD 3.33" parsed as: 123456, 222, 333

std::get_time(tmb, fmt) [C++11] - parses a date/time value stored in tmb of specified format fmt.

tmb - valid pointer to the const std::tm* object where the result will be stored.

fmt - pointer to a null-terminated string const CharT* specifying the conversion format.

#include <sstream> #include <locale>

...

std::tm t = {};

std::istringstream ss("2011-Februar-18 23:12:34");

ss.imbue(std::locale("de_DE.utf-8"));

ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S"); if (ss.fail()) {

std::cout << "Parse failed\n";

}

else {

std::cout << std::put_time(&t, "%c") << '\n';

}

//Possible output:

//Sun Feb 18 23:12:34 2011

GoalKicker.com – C++ Notes for Professionals

75

For more information see the link above.

GoalKicker.com – C++ Notes for Professionals

76