Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/shrink to fit"

From cppreference.com
< cpp‎ | string‎ | basic string
(Fix complexity)
m (improved example to better observe the difference of size and capacity)
Line 33: Line 33:
 
{
 
{
 
     std::string s;
 
     std::string s;
     std::cout << "Default-constructed capacity is " << s.capacity() << '\n';
+
     std::cout << "Default-constructed capacity is " << s.capacity() << " and size " << s.size() << '\n';
     s.resize(100);
+
     for (int i=0; i<42; i++)
     std::cout << "Capacity of a 100-element string is " << s.capacity() << '\n';
+
        s.append(" 42 ");
 +
     std::cout << "Capacity after a couple of appends is " << s.capacity() << " and size " << s.size() << '\n';
 
     s.clear();
 
     s.clear();
     std::cout << "Capacity after clear() is " << s.capacity() << '\n';
+
     std::cout << "Capacity after clear() is " << s.capacity() << " and size " << s.size() << '\n';
 
     s.shrink_to_fit();
 
     s.shrink_to_fit();
     std::cout << "Capacity after shrink_to_fit() is " << s.capacity() << '\n';
+
     std::cout << "Capacity after shrink_to_fit() is " << s.capacity() << " and size " << s.size() << '\n';
 
}
 
}
 
| output=
 
| output=
Default-constructed capacity is 0
+
Default-constructed capacity is 15 and size 0
Capacity of a 100-element string is 100
+
Capacity after a couple of appends is 240 and size 168
Capacity after clear() is 100
+
Capacity after clear() is 240 and size 0
Capacity after shrink_to_fit() is 0
+
Capacity after shrink_to_fit() is 15 and size 0
 
}}
 
}}
  

Revision as of 01:39, 13 September 2019

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
basic_string::shrink_to_fit
(DR*)
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)

 
void shrink_to_fit();
(since C++11)

Requests the removal of unused capacity.

It is a non-binding request to reduce capacity() to size(). It depends on the implementation if the request is fulfilled.

If (and only if) reallocation takes place, all pointers, references, and iterators are invalidated.

Contents

Parameters

(none)

Return value

(none)

Complexity

(unspecified) (until C++17)
Linear in the size of the string (since C++17)


Example

#include <iostream>
#include <string>
 
int main()
{
    std::string s;
    std::cout << "Default-constructed capacity is " << s.capacity() << " and size " << s.size() << '\n';
    for (int i=0; i<42; i++)
        s.append(" 42 ");
    std::cout << "Capacity after a couple of appends is " << s.capacity() << " and size " << s.size() << '\n';
    s.clear();
    std::cout << "Capacity after clear() is " << s.capacity() << " and size " << s.size() << '\n';
    s.shrink_to_fit();
    std::cout << "Capacity after shrink_to_fit() is " << s.capacity() << " and size " << s.size() << '\n';
}

Possible output:

Default-constructed capacity is 15 and size 0
Capacity after a couple of appends is 240 and size 168
Capacity after clear() is 240 and size 0
Capacity after shrink_to_fit() is 15 and size 0

See also

returns the number of characters
(public member function) [edit]
returns the number of characters that can be held in currently allocated storage
(public member function) [edit]