Mega Code Archive

 
Categories / C++ Tutorial / String
 

Functions related to size and capacity

#include <iostream> #include <string> using namespace std; void printStats( const string & ); int main() {    string s;      cout << "Stats before input:\n";    printStats( s );    cout << "\n\nEnter a string: ";    cin >> s;  // delimited by whitespace    cout << "The string entered was: " << s;    cout << "\nStats after input:\n";    printStats( s );    s.resize( s.length() + 10 );    cout << "\n\nStats after resizing by (length + 10):\n";    printStats( s );    cout << endl;    return 0; } void printStats( const string &str ){    cout << "capacity: " << str.capacity()          << "\nmax size: " << str.max_size()         << "\nsize: " << str.size()         << "\nlength: " << str.length()         << "\nempty: " << ( str.empty() ? "true": "false" );              }