std::deque<T,Allocator>:: shrink_to_fit
|
void
shrink_to_fit
(
)
;
|
(constexpr C++26부터) | |
사용되지 않는 용량의 제거를 요청합니다.
이는 시퀀스의 크기를 변경하지 않고 메모리 사용량을 줄이라는 비구속적 요청입니다. 요청이 이행되는지는 구현에 따라 달라집니다.
모든 반복자(
end()
반복자 포함)와 요소에 대한 모든 참조가 무효화됩니다.
|
만약
|
(since C++11) |
목차 |
복잡도
컨테이너 크기에 대해 최대 선형입니다.
예외
non-
CopyInsertable
|
(C++11부터) |
참고 사항
libstdc++에서,
shrink_to_fit()
는
사용할 수 없습니다
C++98 모드에서.
예제
#include <cstddef> #include <deque> #include <iostream> #include <new> // 디버그 출력을 포함한 최소 C++11 할당자 template<class Tp> struct NAlloc { typedef Tp value_type; NAlloc() = default; template<class T> NAlloc(const NAlloc<T>&) {} Tp* allocate(std::size_t n) { n *= sizeof(Tp); std::cout << "allocating " << n << " bytes\n"; return static_cast<Tp*>(::operator new(n)); } void deallocate(Tp* p, std::size_t n) { std::cout << "deallocating " << n*sizeof*p << " bytes\n"; ::operator delete(p); } }; template<class T, class U> bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; } template<class T, class U> bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; } int main() { // std::queue에는 capacity() 함수가 없습니다(std::vector와 달리). // 이 때문에 shrink_to_fit의 동작을 보여주기 위해 // 사용자 정의 할당자를 사용합니다. std::cout << "Default-construct deque:\n"; std::deque<int, NAlloc<int>> deq; std::cout << "\nAdd 300 elements:\n"; for (int i = 1000; i < 1300; ++i) deq.push_back(i); std::cout << "\nPop 100 elements:\n"; for (int i = 0; i < 100; ++i) deq.pop_front(); std::cout << "\nRun shrink_to_fit:\n"; deq.shrink_to_fit(); std::cout << "\nDestroy deque as it goes out of scope:\n"; }
가능한 출력:
Default-construct deque: allocating 64 bytes allocating 512 bytes Add 300 elements: allocating 512 bytes allocating 512 bytes Pop 100 elements: Run shrink_to_fit: allocating 64 bytes allocating 512 bytes allocating 512 bytes deallocating 512 bytes deallocating 512 bytes deallocating 512 bytes deallocating 64 bytes Destroy deque as it goes out of scope: deallocating 512 bytes deallocating 512 bytes deallocating 64 bytes
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 850 | C++98 |
std::deque
에 명시적인 shrink-to-fit 연산이 없었음
|
제공됨 |
| LWG 2033 |
C++98
C++11 |
1. 복잡도 요구사항이 누락됨 (C++98)
2.
T
가
MoveInsertable
요구사항을 충족해야 함 (C++11)
|
1. 추가됨
2. 요구됨 |
| LWG 2223 |
C++98
C++11 |
1. 참조, 포인터, 반복자가 무효화되지 않음 (C++98)
2. 예외 안전성 보장이 없었음 (C++11) |
1. 무효화될 수 있음
2. 추가됨 |
참고 항목
|
요소의 개수를 반환합니다
(public member function) |