CKTIS / C++
Operátor vkladania do výstupného prúdu (`<<`)
Operátor `<<` zapisuje hodnoty do výstupného prúdu alebo mení jeho formátovanie. Typy operandov odlišujú vkladanie do prúdu od bitového posunu doľava.
Verzia: ISO C++23; implementation support may vary
Syntax
out << value;
out << first << second; // (out << first) << second
out << std::boolalpha << flag;
out << (1u << 3); // insert the result of a built-in left shift
// Conventional non-member overload in the namespace of T:
std::ostream& operator<<(std::ostream& out, const T& value);V C++23 výraz std::cout << x vkladá hodnotu do výstupného prúdu, kým 1u << 3 je vstavaný bitový posun doľava. Znak operátora je rovnaký, no typy operandov aj operácie sa líšia. Podľa gramatiky C++ je << asociatívny zľava, takže sa out << a << b zoskupuje ako (out << a) << b bez ohľadu na návratový typ vybraného preťaženia. Pre lvalue out typu std::ostream bežné preťaženia vracajú referenciu std::ostream& na ten istý prúd, takže nasledujúce vkladanie môže zapisovať do toho istého prúdu. Medzery ani konce riadkov sa nepridávajú automaticky.
Čísla používajú členské preťaženia, char a text nečlenské preťaženia. Používaj out << x namiesto out.operator<<(x), pretože explicitné volanie členskej funkcie vynecháva nečlenské funkcie. Pri vkladaní const char* musí ukazovateľ odkazovať na platný reťazec ukončený nulovým znakom. std::string a std::string_view používajú svoju dĺžku, takže nulový znak vnútri výstup neukončí. std::string_view údaje nevlastní: znaky, na ktoré odkazuje, musia zostať platné počas celého vkladania.
Formátovanie patrí prúdu. out << std::boolalpha aj out.setf(std::ios::boolalpha) zapínajú názvy logických hodnôt podľa miestnych nastavení (true a false pri klasickom nastavení). Príznak zostáva nastavený, kým ho nevypneš, napríklad pomocou std::noboolalpha. Naopak, std::setw(n) nastaví minimálnu šírku poľa, ktorú bežné preťaženia pre znaky, reťazce a čísla po použití vynulujú.
out << '\n' vloží znak nového riadka bez výslovnej požiadavky na vyprázdnenie vyrovnávacej pamäte. std::endl vloží znak nového riadka a zavolá flush(). Vrátenie prúdu nepreukazuje úspešný zápis: potom skontroluj if (!out). Chyba môže nastaviť failbit alebo badbit a v závislosti od masky výnimiek prúdu môže vyvolať výnimku.
Pre vlastný typ T definuj nečlenský operator<<(std::ostream&, const T&) v mennom priestore typu, nie v std. Vyhľadávanie závislé od argumentov (ADL) ho tam môže nájsť. Zapisuj do odovzdaného prúdu a vráť naň referenciu, aby sa dalo vkladanie reťaziť.
Príklady
#include <ios>
#include <iostream>
#include <locale>
#include <ostream>
#include <string_view>
namespace report {
struct Result {
std::string_view label;
bool ok;
};
std::ostream& operator<<(std::ostream& out, const Result& result) {
return out << result.label << ": " << result.ok;
}
} // namespace report
int main() {
std::cout.imbue(std::locale::classic());
std::cout.setf(std::ios::boolalpha);
const report::Result result{std::string_view{"ABC", 2}, true};
std::cout << result << ' ' << false << '\n';
std::cout << "shift=" << (1u << 3) << '\n';
}Výsledok
AB: true false shift=8
`report::Result` vypíše `AB`, lebo pohľad zahŕňa len prvé dva znaky `ABC`. Príznak `boolalpha` platí vo vlastnom preťažení aj pre nasledujúce `false`. Na druhom riadku zátvorky oddeľujú bitový posun s výsledkom `8` od vloženia výsledku do prúdu.
Súvisiace lekcie
- C++ · Lekcia 1 · Pokročilá · Verzia: 3
- C++ · Lekcia 2 · Pokročilá · Verzia: 3
- C++ · Lekcia 3 · Pokročilá · Verzia: 3
- C++ · Lekcia 4 · Pokročilá · Verzia: 3
- C++ · Lekcia 5 · Pokročilá · Verzia: 3
- C++ · Lekcia 6 · Pokročilá · Verzia: 3
- C++ · Lekcia 7 · Pokročilá · Verzia: 3
- C++ · Lekcia 8 · Pokročilá · Verzia: 3
- C++ · Lekcia 9 · Pokročilá · Verzia: 3
- C++ · Lekcia 10 · Pokročilá · Verzia: 3
- C++ · Lekcia 11 · Pokročilá · Verzia: 3
- C++ · Lekcia 12 · Pokročilá · Verzia: 3
- C++ · Lekcia 13 · Pokročilá · Verzia: 3
- C++ · Lekcia 14 · Pokročilá · Verzia: 3
- C++ · Lekcia 15 · Pokročilá · Verzia: 3
- C++ · Lekcia 16 · Pokročilá · Verzia: 3
- C++ · Lekcia 17 · Pokročilá · Verzia: 3
- C++ · Lekcia 18 · Pokročilá · Verzia: 3
- C++ · Lekcia 19 · Pokročilá · Verzia: 3
- C++ · Lekcia 20 · Pokročilá · Verzia: 3
- C++ · Lekcia 21 · Pokročilá · Verzia: 3
- C++ · Lekcia 22 · Pokročilá · Verzia: 3
- C++ · Lekcia 23 · Pokročilá · Verzia: 3
- C++ · Lekcia 24 · Pokročilá · Verzia: 3
- C++ · Lekcia 25 · Pokročilá · Verzia: 3
- C++ · Lekcia 26 · Pokročilá · Verzia: 3
- C++ · Lekcia 27 · Pokročilá · Verzia: 3
- C++ · Lekcia 28 · Pokročilá · Verzia: 3
- C++ · Lekcia 29 · Pokročilá · Verzia: 3
- C++ · Lekcia 30 · Pokročilá · Verzia: 3
- C++ · Lekcia 31 · Pokročilá · Verzia: 3
- C++ · Lekcia 32 · Pokročilá · Verzia: 3
- C++ · Lekcia 33 · Pokročilá · Verzia: 3
- C++ · Lekcia 34 · Pokročilá · Verzia: 3
- C++ · Lekcia 35 · Pokročilá · Verzia: 3
- C++ · Lekcia 36 · Pokročilá · Verzia: 3
- C++ · Lekcia 37 · Pokročilá · Verzia: 3
- C++ · Lekcia 38 · Pokročilá · Verzia: 3
- C++ · Lekcia 39 · Pokročilá · Verzia: 3
- C++ · Lekcia 40 · Pokročilá · Verzia: 3
- C++ · Lekcia 41 · Pokročilá · Verzia: 3
- C++ · Lekcia 42 · Pokročilá · Verzia: 3
- C++ · Lekcia 43 · Pokročilá · Verzia: 3
- C++ · Lekcia 44 · Pokročilá · Verzia: 3
- C++ · Lekcia 45 · Pokročilá · Verzia: 3
- C++ · Lekcia 46 · Pokročilá · Verzia: 3
- C++ · Lekcia 47 · Pokročilá · Verzia: 3
- C++ · Lekcia 48 · Pokročilá · Verzia: 3
- C++ · Lekcia 49 · Pokročilá · Verzia: 3
- C++ · Lekcia 50 · Pokročilá · Verzia: 3
- C++ · Lekcia 51 · Pokročilá · Verzia: 3
- C++ · Lekcia 52 · Pokročilá · Verzia: 3
- C++ · Lekcia 53 · Pokročilá · Verzia: 3
- C++ · Lekcia 54 · Pokročilá · Verzia: 3
- C++ · Lekcia 55 · Pokročilá · Verzia: 3
- C++ · Lekcia 56 · Pokročilá · Verzia: 3
- C++ · Lekcia 57 · Pokročilá · Verzia: 3
- C++ · Lekcia 58 · Pokročilá · Verzia: 3
- C++ · Lekcia 59 · Pokročilá · Verzia: 3
- C++ · Lekcia 60 · Pokročilá · Verzia: 3
Zdroje
- WG21 N4950 — C++23 working draft
- Formatted output functions [ostream.formatted]
- Shift operators [expr.shift]
- Operators in expressions [over.match.oper]
- Argument-dependent name lookup [basic.lookup.argdep]
- String view inserters and extractors [string.view.io]
- String inserters and extractors [string.io]
- fmtflags manipulators [fmtflags.manip]
- Standard manipulators [std.manip]
- Standard basic_ostream manipulators [ostream.manip]
- basic_ios member functions [basic.ios.members]