Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/capacity"

From cppreference.com
< cpp‎ | string‎ | basic string
m (Undo revision 124704 by 79.156.56.92 (talk) recovery #2)
m (Synopsis: {{ddcla}})
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/string/basic_string/title | capacity}}
+
{{cpp/string/basic_string/title|capacity}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
{{dcl begin}}
+
{{ddcla|noexcept=c++11|constexpr=c++20|
{{dcl rev multi | until1=c++11 | dcl1=
+
 
size_type capacity() const;
 
size_type capacity() const;
| until2=c++20 | dcl2=
 
size_type capacity() const noexcept;
 
| dcl3=
 
constexpr size_type capacity() const noexcept;
 
 
}}
 
}}
{{dcl end}}
 
  
 
Returns the number of characters that the string has currently allocated space for.  
 
Returns the number of characters that the string has currently allocated space for.  
Line 20: Line 14:
  
 
===Complexity===
 
===Complexity===
Constant
+
Constant.
  
 
===Notes===
 
===Notes===
Memory locations obtained from the allocator but not available for storing any element are not counted in the allocated storage. Note that the null terminator is not an element of the {{tt|basic_string}}.
+
Memory locations obtained from the allocator but not available for storing any element are not counted in the allocated storage. Note that the null terminator is not an element of the {{lc|std::basic_string}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
 +
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
 
#include <string>
 
#include <string>
Line 33: Line 28:
 
void show_capacity(std::string const& s)
 
void show_capacity(std::string const& s)
 
{
 
{
     std::cout << "'" << s << "' has capacity " << s.capacity() << ".\n";
+
     std::cout << std::quoted(s) << " has capacity " << s.capacity() << ".\n";
 
}
 
}
  
Line 43: Line 38:
 
     s += " is an example string.";
 
     s += " is an example string.";
 
     show_capacity(s);
 
     show_capacity(s);
 +
 +
    s.clear();
 +
    show_capacity(s);
 +
 +
    std::cout << "\nDemonstrate the capacity's growth policy."
 +
                "\nSize:  Capacity:  Ratio:\n" << std::left;
 +
 +
    std::string g;
 +
    auto old_cap{g.capacity()};
 +
 +
    for (int mark{}; mark != 5; ++mark)
 +
    {
 +
        while (old_cap == g.capacity())
 +
            g.push_back('.');
 +
 +
        std::cout << std::setw( 7) << g.size()
 +
                  << std::setw(11) << g.capacity()
 +
                  << std::setw(10) << g.capacity() / static_cast<float>(old_cap) << '\n';
 +
 +
        old_cap = g.capacity();
 +
    }
 
}
 
}
 
|p=true
 
|p=true
 
|output=
 
|output=
'Exemplar' has capacity 15.
+
"Exemplar" has capacity 15.
'Exemplar is an example string.' has capacity 30.
+
"Exemplar is an example string." has capacity 30.
 +
"" has capacity 30.
 +
 
 +
Demonstrate the capacity's growth policy.
 +
Size:  Capacity:  Ratio:
 +
16    30        2
 +
31    60        2
 +
61    120        2
 +
121    240        2
 +
241    480        2
 
}}
 
}}
===See also===
 
  
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc size}}
+
{{dsc inc|cpp/string/basic_string/dsc size}}
{{dsc inc | cpp/string/basic_string/dsc reserve}}
+
{{dsc inc|cpp/string/basic_string/dsc reserve}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Latest revision as of 13:47, 18 October 2024

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
basic_string::capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
size_type capacity() const;
(noexcept since C++11)
(constexpr since C++20)

Returns the number of characters that the string has currently allocated space for.

Contents

[edit] Parameters

(none)

[edit] Return value

Capacity of the currently allocated storage, i.e. the storage available for storing elements.

[edit] Complexity

Constant.

[edit] Notes

Memory locations obtained from the allocator but not available for storing any element are not counted in the allocated storage. Note that the null terminator is not an element of the std::basic_string.

[edit] Example

#include <iomanip>
#include <iostream>
#include <string>
 
void show_capacity(std::string const& s)
{
    std::cout << std::quoted(s) << " has capacity " << s.capacity() << ".\n";
}
 
int main()
{
    std::string s{"Exemplar"};
    show_capacity(s);
 
    s += " is an example string.";
    show_capacity(s);
 
    s.clear();
    show_capacity(s);
 
    std::cout << "\nDemonstrate the capacity's growth policy."
                 "\nSize:  Capacity:  Ratio:\n" << std::left;
 
    std::string g;
    auto old_cap{g.capacity()};
 
    for (int mark{}; mark != 5; ++mark)
    {
        while (old_cap == g.capacity())
            g.push_back('.');
 
        std::cout << std::setw( 7) << g.size()
                  << std::setw(11) << g.capacity()
                  << std::setw(10) << g.capacity() / static_cast<float>(old_cap) << '\n';
 
        old_cap = g.capacity();
    }
}

Possible output:

"Exemplar" has capacity 15.
"Exemplar is an example string." has capacity 30.
"" has capacity 30.
 
Demonstrate the capacity's growth policy.
Size:  Capacity:  Ratio:
16     30         2
31     60         2
61     120        2
121    240        2
241    480        2

[edit] See also

returns the number of characters
(public member function) [edit]
reserves storage
(public member function) [edit]