C++
Streammanipulators
Zoeken…
Invoering
Manipulators zijn speciale hulpfuncties die helpen bij het regelen van invoer- en uitvoerstromen met operator >> of operator << .
Ze kunnen allemaal worden opgenomen door #include <iomanip> .
Opmerkingen
Manipulators kunnen op een andere manier worden gebruikt. Bijvoorbeeld:
-
os.width(n);is gelijk aanos << std::setw(n);
is.width(n);is gelijk aanis >> std::setw(n);
-
os.precision(n);is gelijk aanos << std::setprecision(n);
is.precision(n);is gelijk aanis >> std::setprecision(n);
-
os.setfill(c);is gelijk aanos << std::setfill(c);
-
str >> std::setbase(base);ofstr << std::setbase(base);is gelijk aan
str.setf(base == 8 ? std::ios_base::oct :
base == 10 ? std::ios_base::dec :
base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags(0),
std::ios_base::basefield);
-
os.setf(std::ios_base::flag);is gelijk aanos << std::flag;
is.setf(std::ios_base::flag);is gelijk aanis >> std::flag;
os.unsetf(std::ios_base::flag);is gelijk aanos << std::no ## flag;
is.unsetf(std::ios_base::flag);is gelijk aanis >> std::no ## flag;
(waar ## - operator voor aaneenschakeling is )
voor volgendeflag:boolalpha,showbase,showpoint,showpos,skipws,uppercase.
-
std::ios_base::basefield.
Voorflags:dec,hexenoct:
-
os.setf(std::ios_base::flag, std::ios_base::basefield);is gelijk aanos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::basefield);is gelijk aanis >> std::flag;
(1) -
str.unsetf(std::ios_base::flag, std::ios_base::basefield);is gelijk aanstr.setf(std::ios_base::fmtflags(0), std::ios_base::basefield);
(2)
-
std::ios_base::adjustfield.
Voorflags:left,righteninternal:
-
os.setf(std::ios_base::flag, std::ios_base::adjustfield);is gelijk aanos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::adjustfield);is gelijk aanis >> std::flag;
(1) -
str.unsetf(std::ios_base::flag, std::ios_base::adjustfield);is gelijk aanstr.setf(std::ios_base::fmtflags(0), std::ios_base::adjustfield);
(2)
(1) Als de vlag van het eerder ingestelde overeenkomstige veld al is uitgeschakeld door unsetf .
(2) Als flag is ingesteld.
-
std::ios_base::floatfield.
-
os.setf(std::ios_base::flag, std::ios_base::floatfield);is gelijk aanos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::floatfield);is gelijk aanis >> std::flag;
voorflags:fixedenscientific. -
os.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);is gelijk aanos << std::defaultfloat;
is.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);is gelijk aanis >> std::defaultfloat;
-
str.setf(std::ios_base::fmtflags(0), std::ios_base::flag);is gelijk aanstr.unsetf(std::ios_base::flag)
voorflags:basefield,adjustfield,floatfield.
-
os.setf(mask)gelijk aanos << setiosflags(mask);
is.setf(mask)gelijk aanis >> setiosflags(mask);
os.unsetf(mask)gelijk aanos << resetiosflags(mask);
is.unsetf(mask)gelijk aanis >> resetiosflags(mask);
Voor bijna allemaskvan het typestd::ios_base::fmtflags.
Streammanipulators
std::boolalpha en std::noboolalpha - schakel tussen tekstuele en numerieke weergave van booleans.
std::cout << std::boolalpha << 1;
// Output: true
std::cout << std::noboolalpha << false;
// Output: 0
bool boolValue;
std::cin >> std::boolalpha >> boolValue;
std::cout << "Value \"" << std::boolalpha << boolValue
<< "\" was parsed as " << std::noboolalpha << boolValue;
// Input: true
// Output: Value "true" was parsed as 0
std::showbase en std::noshowbase - std::noshowbase of het voorvoegsel dat een numerieke basis aangeeft wordt gebruikt.
std::dec (decimaal), std::hex (hexadecimaal) en std::oct (octaal) - worden gebruikt voor het wijzigen van de basis voor gehele getallen.
#include <sstream>
std::cout << std::dec << 29 << ' - '
<< std::hex << 29 << ' - '
<< std::showbase << std::oct << 29 << ' - '
<< std::noshowbase << 29 '\n';
int number;
std::istringstream("3B") >> std::hex >> number;
std::cout << std::dec << 10;
// Output: 22 - 1D - 35 - 035
// 59
Standaardwaarden zijn std::ios_base::noshowbase en std::ios_base::dec .
Als je meer wilt zien over std::istringstream bekijk dan de < sstream > header.
std::uppercase en std::nouppercase - std::nouppercase of hoofdletters worden gebruikt in uitvoer met drijvende komma en hexadecimale integer. Heeft geen effect op invoerstromen.
std::cout << std::hex << std::showbase
<< "0x2a with nouppercase: " << std::nouppercase << 0x2a << '\n'
<< "1e-10 with uppercase: " << std::uppercase << 1e-10 << '\n'
}
// Output: 0x2a with nouppercase: 0x2a
// 1e-10 with uppercase: 1E-10
Standaard is std::nouppercase .
std::setw(n) - verandert de breedte van het volgende invoer / uitvoer-veld naar exact n .
De eigenschap width n wordt opnieuw ingesteld op 0 wanneer sommige functies worden aangeroepen (volledige lijst is hier ).
std::cout << "no setw:" << 51 << '\n'
<< "setw(7): " << std::setw(7) << 51 << '\n'
<< "setw(7), more output: " << 13
<< std::setw(7) << std::setfill('*') << 67 << ' ' << 94 << '\n';
char* input = "Hello, world!";
char arr[10];
std::cin >> std::setw(6) >> arr;
std::cout << "Input from \"Hello, world!\" with setw(6) gave \"" << arr << "\"\n";
// Output: 51
// setw(7): 51
// setw(7), more output: 13*****67 94
// Input: Hello, world!
// Output: Input from "Hello, world!" with setw(6) gave "Hello"
Standaard is std::setw(0) .
std::left , std::right en std::internal - wijzig de standaardpositie van de std::ios_base::adjustfield door std::ios_base::adjustfield te stellen op std::ios_base::left , std::ios_base::right en std::ios_base::internal overeenkomstig. std::left en std::right toepassing op elke output, std::internal - voor integer, floating-point en monetaire output. Heeft geen effect op invoerstromen.
#include <locale>
...
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::left << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, false) << '\n';
// Output:
// flt: -9.87**********
// hex: 41*************
// $: $3.67**********
// usd: USD *3.67******
// usd: $3.67
std::cout << std::internal << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: -**********9.87
// hex: *************41
// $: $3.67**********
// usd: USD *******3.67
// usd: USD 3.67
std::cout << std::right << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: **********-9.87
// hex: *************41
// $: **********$3.67
// usd: ******USD *3.67
// usd: USD 3.67
Standaard is std::left .
std::fixed , std::scientific , std::hexfloat [C ++ 11] en std::defaultfloat [C ++ 11] - wijzig de opmaak voor invoer / uitvoer met drijvende komma.
std::fixed stelt de std::ios_base::floatfield op std::ios_base::fixed ,
std::scientific - naar std::ios_base::scientific ,
std::hexfloat - to std::ios_base::fixed | std::ios_base::scientific en
std::defaultfloat - to std::ios_base::fmtflags(0) .
fmtflags
#include <sstream>
...
std::cout << '\n'
<< "The number 0.07 in fixed: " << std::fixed << 0.01 << '\n'
<< "The number 0.07 in scientific: " << std::scientific << 0.01 << '\n'
<< "The number 0.07 in hexfloat: " << std::hexfloat << 0.01 << '\n'
<< "The number 0.07 in default: " << std::defaultfloat << 0.01 << '\n';
double f;
std::istringstream is("0x1P-1022");
double f = std::strtod(is.str().c_str(), NULL);
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output:
// The number 0.01 in fixed: 0.070000
// The number 0.01 in scientific: 7.000000e-02
// The number 0.01 in hexfloat: 0x1.1eb851eb851ecp-4
// The number 0.01 in default: 0.07
// Parsing 0x1P-1022 as hex gives 2.22507e-308
Standaard is std::ios_base::fmtflags(0) .
Er is een bug in sommige compilers die dit veroorzaakt
double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output: Parsing 0x1P-1022 as hex gives 0
std::showpoint en std::noshowpoint - std::noshowpoint of de decimale punt altijd wordt opgenomen in de weergave met drijvende komma. Heeft geen effect op invoerstromen.
std::cout << "7.0 with showpoint: " << std::showpoint << 7.0 << '\n'
<< "7.0 with noshowpoint: " << std::noshowpoint << 7.0 << '\n';
// Output: 1.0 with showpoint: 7.00000
// 1.0 with noshowpoint: 7
Standaard is std::showpoint .
std::showpos en std::noshowpos - controle weergave van het + teken in niet-negatieve uitvoer. Heeft geen effect op invoerstromen.
std::cout << "With showpos: " << std::showpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n'
<< "Without showpos: " << std::noshowpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n';
// Output: With showpos: +0 -2.718 +17
// Without showpos: 0 -2.718 17
Standaard als std::noshowpos .
std::unitbuf , std::nounitbuf - controleer de std::nounitbuf na elke bewerking. Heeft geen effect op de invoerstroom. std::unitbuf veroorzaakt blozen.
std::setbase(base) - stelt de numerieke basis van de stream in.
std::setbase(8) gelijk aan het instellen van std::ios_base::basefield naar std::ios_base::oct ,
std::setbase(16) - naar std::ios_base::hex ,
std::setbase(10) - to std::ios_base::dec .
Als base anders is dan 8 , 10 of 16 dan stelt std::ios_base::basefield in op std::ios_base::fmtflags(0) . Het betekent decimale uitvoer en prefix-afhankelijke invoer.
Standaard is std::ios_base::basefield std::ios_base::dec vervolgens standaard std::setbase(10) .
std::setprecision(n) - wijzigt drijvende komma precisie.
#include <cmath>
#include <limits>
...
typedef std::numeric_limits<long double> ld;
const long double pi = std::acos(-1.L);
std::cout << '\n'
<< "default precision (6): pi: " << pi << '\n'
<< " 10pi: " << 10 * pi << '\n'
<< "std::setprecision(4): 10pi: " << std::setprecision(4) << 10 * pi << '\n'
<< " 10000pi: " << 10000 * pi << '\n'
<< "std::fixed: 10000pi: " << std::fixed << 10000 * pi << std::defaultfloat << '\n'
<< "std::setprecision(10): pi: " << std::setprecision(10) << pi << '\n'
<< "max-1 radix precicion: pi: " << std::setprecision(ld::digits - 1) << pi << '\n'
<< "max+1 radix precision: pi: " << std::setprecision(ld::digits + 1) << pi << '\n'
<< "significant digits prec: pi: " << std::setprecision(ld::digits10) << pi << '\n';
// Output:
// default precision (6): pi: 3.14159
// 10pi: 31.4159
// std::setprecision(4): 10pi: 31.42
// 10000pi: 3.142e+04
// std::fixed: 10000pi: 31415.9265
// std::setprecision(10): pi: 3.141592654
// max-1 radix precicion: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// max+1 radix precision: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// significant digits prec: pi: 3.14159265358979324
Standaard is std::setprecision(6) .
std::setiosflags(mask) en std::resetiosflags(mask) - vlaggen instellen en wissen die zijn opgegeven in mask van std::ios_base::fmtflags type.
#include <sstream>
...
std::istringstream in("10 010 10 010 10 010");
int num1, num2;
in >> std::oct >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::oct gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 8 8
in >> std::dec >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::dec gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 10 10
in >> std::resetiosflags(std::ios_base::basefield) >> num1 >> num2;
std::cout << "Parsing \"10 010\" with autodetect gives: " << num1 << ' ' << num2 << '\n';
// Parsing "10 010" with autodetect gives: 10 8
std::cout << std::setiosflags(std::ios_base::hex |
std::ios_base::uppercase |
std::ios_base::showbase) << 42 << '\n';
// Output: OX2A
std::skipws en std::noskipws - controle van het overslaan van voorloopruimte door de geformatteerde invoerfuncties. Heeft geen effect op outputstromen.
#include <sstream>
...
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
Standaard is std::ios_base::skipws .
std::quoted(s[, delim[, escape]]) [C ++ 14] - voegt aanhalingstekens met ingesloten spaties in of extraheert deze.
s - de tekenreeks die moet worden ingevoegd of geëxtraheerd.
delim - het teken dat als scheidingsteken moet worden gebruikt, " standaard.
escape - het teken dat als het escape-teken moet worden gebruikt, standaard \ .
#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]
Zie bovenstaande link voor meer informatie.
Uitgangsstroommanipulators
std::ends - voegt een null-teken '\0' om de stream uit te voeren. Meer formeel ziet de verklaring van deze manipulator eruit
template <class charT, class traits>
std::basic_ostream<charT, traits>& ends(std::basic_ostream<charT, traits>& os);
en deze manipulator plaatst karakter door os.put(charT()) aan te roepen wanneer gebruikt in een uitdrukking
os << std::ends;
std::endl en std::flush beide out.flush() out door out.flush() aan te roepen. Het veroorzaakt onmiddellijk output. Maar std::endl voegt het einde van de regel '\n' voor het spoelen.
std::cout << "First line." << std::endl << "Second line. " << std::flush
<< "Still second line.";
// Output: First line.
// Second line. Still second line.
std::setfill(c) - verandert het vulteken in c . Vaak gebruikt met 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 een uitdrukking out << std::put_money(mon, intl) , converteert de geldwaarde mon (van long double of std::basic_string type) naar zijn karakterrepresentatie zoals gespecificeerd door het std::money_put facet van de locale die momenteel is geïnfundeerd in out . Gebruik internationale valutareeksen als intl true , gebruik anders valutasymbolen.
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] - formatteert en voert een datum / tijdwaarde uit naar std::tm volgens het opgegeven formaat fmt .
tmb - pointer naar de kalender tijdstructuur const std::tm* zoals verkregen van localtime() of gmtime() .
fmt - pointer naar een nul-beëindigde string const CharT* die het formaat van de conversie const CharT* .
#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
Zie bovenstaande link voor meer informatie.
Input stream manipulators
std::ws - verbruikt voorloopruimten in invoerstroom. Het verschilt van 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 een uitdrukking in >> std::get_money(mon, intl) ontleedt de tekeninvoer als een geldwaarde, zoals gespecificeerd door het facet std::money_get van de locale die momenteel is doordrenkt in , en slaat de waarde op in mon (van long double of std::basic_string type). Manipulator verwacht verplichte internationale valutareeksen als intl true , verwacht anders optionele valutasymbolen.
#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] - parseert een datum / tijd-waarde opgeslagen in tmb van gespecificeerd formaat fmt .
tmb - geldige aanwijzer naar het object const std::tm* waar het resultaat wordt opgeslagen.
fmt - pointer naar een nul-beëindigde string const CharT* die het conversieformaat const CharT* .
#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
Zie bovenstaande link voor meer informatie.