CKTIS / C++
`std` — menný priestor štandardnej knižnice C++
Nauč sa čítať názvy ako `std::cout` a `std::chrono::seconds`, sprístupniť ich deklarácie a bezpečne skracovať zápis. Príklady z kurzu odlišujú pravidlá menného priestoru od správania konkrétnych rozhraní.
Verzia: ISO C++23; implementation support may vary
Syntax
#include <iostream>
#include <iomanip>
// In a function:
std::cout << std::fixed << std::setprecision(2) << 3.5 << '\n';
// Alternatively, introduce one name into the block:
using std::setprecision;
std::cout << std::fixed << setprecision(2) << 3.5 << '\n';std je menný priestor, nie kľúčové slovo ani hlavička. V std::cout operátor :: kvalifikuje meno; ::std::cout výslovne vyberá globálne std. std::chrono je vnorený priestor. Globálne funkcie na alokáciu operator new a dealokáciu operator delete sú výnimky z predpony std::. Vlož príslušnú hlavičku priamo. V C++23 príkaz import std; importuje knižničný modul, nie menný priestor; podpora závisí od implementácie a import nesprístupní makrá z hlavičiek.
using std::cout; umožní použiť jedno meno bez kvalifikácie v danom rozsahu platnosti. using namespace std; ovplyvňuje nekvalifikované vyhľadávanie širšie a môže spôsobiť nejednoznačnosť; vyhni sa tejto direktíve v globálnom rozsahu hlavičiek. Ani jedna forma using nenahrádza hlavičku či import. Pridávanie deklarácií alebo definícií do std a jeho vnorených priestorov spravidla vedie k nedefinovanému správaniu. Špecializácia šablóny triedy, napríklad std::hash pre vlastný typ, nesmie byť zakázaná a musí spĺňať požiadavky pôvodnej šablóny.
Kvalifikácia mena neurčuje požiadavky rozhrania. Napríklad oba vstupné rozsahy pre std::set_union musia byť zoradené podľa rovnakého usporiadania, aké algoritmus používa pri danom volaní.
Príklady
#include <iomanip>
#include <iostream>
int main() {
using std::setprecision;
std::cout << std::fixed << setprecision(2) << 3.5 << '\n';
}Výsledok
3.50
`using std::setprecision;` skráti iba toto meno v `main`. `std::fixed` zostane kvalifikované a výstup je `3.50` nasledované znakom nového riadka.
Súvisiace lekcie
- C++ · Lekcia 2 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 10 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 11 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 16 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 18 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 19 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 20 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 26 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 30 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 32 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 36 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 38 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 41 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 42 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 46 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 47 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 48 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 50 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 51 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 52 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 55 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 56 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 57 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 58 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 59 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 61 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 65 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 66 · Stredne pokročilá · Verzia: 2
- C++ · Lekcia 67 · Stredne pokročilá · Verzia: 2
Zdroje
- WG21 N4950 — Working Draft, Standard for Programming Language C++
- Library contents [contents]
- Namespace std [namespace.std]
- Using namespace directive [namespace.udir]
- The using declaration [namespace.udecl]
- Modules [std.modules]
- Input/output library [input.output]
- General utilities library [utilities]
- Strings library [strings]
- Iterators library [iterators]
- Time library [time]
- Concurrency support library [thread]