std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
void shrink_to_fit(); |
( C++20) | |
constexpr void shrink_to_fit(); |
( C++20) | |
.
capacity() size(). , .
( ) , , .
()
()
|
( ) |
( C++17) |
|
. |
( C++17) |
libstdc++, shrink_to_fit() C++98.
#include <iostream>
#include <string>
int main()
{
std::string s;
std::cout << " std::string " << sizeof s << " \n"
<< " " << s.capacity()
<< " " << s.size() << '\n';
for (int i = 0; i < 42; i++)
s.append(" 42 ");
std::cout << " 42 " << s.capacity()
<< " " << s.size() << '\n';
s.clear();
std::cout << " clear() " << s.capacity()
<< " " << s.size() << '\n';
s.shrink_to_fit();
std::cout << " shrink_to_fit() " << s.capacity()
<< " " << s.size() << '\n';
}
:
GCC output:
std::string 32
15 0
42 240 168
clear() 240 0
shrink_to_fit() 15 0
clang output (with -stdlib=libc++):
std::string 24
22 0
42 191 168
clear() 191 0
shrink_to_fit() 22 0
C++:
| LWG 755 | C++98 | std::string
|
| (public -) | |
| , (public -) | |
| (public -) |