Sdílet prostřednictvím


basic_string::size a basic_string::resize

Znázorňuje použití basic_string::size a basic_string::resize funkce standardní šablonu knihovny (STL) v jazyce C++.

size_type size( ) const;
   void resize(
      size_type n, 
      E c = E( )
   );

Poznámky

[!POZNÁMKA]

Názvy tříd/parametr v prototyp verze v záhlaví souboru neodpovídají.Některé byly upraveny, aby se zlepšila čitelnost.

basic_string::size STL funkce vrací délku sekvence.basic_string::resize Funkce STL změní velikost na délku uvedenou první parametr.Pokud posloupnost delší, je funkce připojí prvky s hodnotou druhý parametr.Výchozí hodnota je null.Výstup ukázkový kód ukazuje prostory pro znaky null.operátor << přečte velikost řetězce a výstupy jednotlivých znaků v řetězci, jeden v čase.

Příklad

// size.cpp
// compile with: /EHsc
// 
// Functions:
//    size()
//    resize() ; Defined in header xstring which is included indirectly.
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string TestString = "1111122222333334444455555";
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;

   TestString.resize(5);
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;

   TestString.resize(10);
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl
        << endl;

   TestString.resize(15,'6');
   cout << "[" << TestString << "]" << endl
        << "size: " << TestString.size() << endl;
}

Vzorový výstup

[1111122222333334444455555]
size: 25

[11111]
size: 5

[11111     ]
size: 10

[11111     66666]
size: 15

Požadavky

Záhlaví: <string>

Viz také

Koncepty

Standardní šablona knihovny vzorků