Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::deque<T,Allocator>::shrink_to_fit

      From cppreference.com
      <cpp‎ |container‎ |deque

      [edit template]
       
       
       
      std::deque
      Member types
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      void shrink_to_fit();
      (constexpr since C++26)

      Requests the removal of unused capacity.

      It is a non-binding request to reduce the memory usage without changing the size of the sequence. It depends on the implementation whether the request is fulfilled.

      All iterators (including theend() iterator) and all references to the elements are invalidated.

      IfT is notMoveInsertable intostd::deque<T, Allocator>, the behavior is undefined.

      (since C++11)

      Contents

      [edit]Complexity

      At most linear in the size of the container.

      Exceptions

      If an exception is thrown other than by the move constructor of a non-CopyInsertableT, there are no effects.

      (since C++11)

      [edit]Notes

      In libstdc++,shrink_to_fit() isnot available in C++98 mode.

      [edit]Example

      [edit]
      Run this code
      #include <cstddef>#include <deque>#include <iostream>#include <new> // Minimal C++11 allocator with debug output.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";returnstatic_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>&){returntrue;}template<class T,class U>bool operator!=(const NAlloc<T>&,const NAlloc<U>&){returnfalse;} int main(){// std::queue has no capacity() function (like std::vector).// Because of this, we use a custom allocator to show the// working of 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";}

      Possible output:

      Default-construct deque:allocating 64 bytesallocating 512 bytes Add 300 elements:allocating 512 bytesallocating 512 bytes Pop 100 elements: Run shrink_to_fit:allocating 64 bytesallocating 512 bytesallocating 512 bytesdeallocating 512 bytesdeallocating 512 bytesdeallocating 512 bytesdeallocating 64 bytes Destroy deque as it goes out of scope:deallocating 512 bytesdeallocating 512 bytesdeallocating 64 bytes

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 850C++98std::deque lacked explicit shrink-to-fit operationsprovided
      LWG 2033C++98
      C++11
      1. the complexity requirement was missing (C++98)
      2.T was not required to beMoveInsertable (C++11)
      1. added
      2. required
      LWG 2223C++98
      C++11
      1. references, pointers, and iterators were not invalidated (C++98)
      2. there was no exception safety guarantee (C++11)
      1. they may be invalidated
      2. added

      [edit]See also

      returns the number of elements
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/deque/shrink_to_fit&oldid=135179"

      [8]ページ先頭

      ©2009-2025 Movatter.jp