Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inplace_vector

      From cppreference.com
      <cpp‎ |container
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      Defined in header<inplace_vector>
      template<

         class T,
         std::size_t N

      >struct inplace_vector;
      (since C++26)

      inplace_vector is a dynamically-resizable array with contiguous inplace storage. The elements of typeT are stored and properly aligned within the object itself. The capacity of the internal storage is fixed at compile-time and is equal toN.

      The elements are stored contiguously, which means that elements can be accessed not only through iterators or random-accessoperator[], but also using offsets to regular pointers to elements. A pointer to an element of aninplace_vector may be passed to any function that expects a pointer to an element of a C-array.

      Theinplace_vector modelsContainer,ReversibleContainer,ContiguousContainer, andSequenceContainer, including most of theoptional sequence container requirements, except that thepush_front,emplace_front,pop_front, andprepend_range member functions are not provided.

      For any positiveN,std::inplace_vector<T, N>::iterator andstd::inplace_vector<T, N>::const_iterator meet theConstexprIterator requirements.

      The specializationstd::inplace_vector<T,0> isTriviallyCopyable and is empty.std::is_trivially_default_constructible_v<std::inplace_vector<T,0>> is alsotrue.

      Any member function ofstd::inplace_vector<T, N> that would cause insertion beyond the capacityN throwsstd::bad_alloc.

      The complexity of common operations oninplace_vectors is as follows:

      • Random access to an element viaoperator[] orat() – constant:𝓞(1).
      • Insertion or removal of an element at the end – constant:𝓞(1).
      • Insertion or removal of elements at the end – linear in the number of elements inserted/removed:𝓞(n).
      • Insertion or removal of elements in the beginning or in the middle – linear in the number of elements inserted/removed plus the distance to the end of the vector:𝓞(n).

      Contents

      [edit]Iterator invalidation

      std::inplace_vector iterator invalidation guarantees differ fromstd::vector:

      • moving aninplace_vector invalidates all iterators;
      • swapping twoinplace_vectors invalidates all iterators (during swap, the iterator will continue to point to the same array element, and may thus change its value).

      The following member functions potentially invalidate iterators:operator=,assign,assign_range,clear,emplace,erase,insert,insert_range,pop_back,resize, andswap.

      The following member functions potentially invalidateend iterator only:append_range,emplace_back,push_back,try_append_range,try_emplace_back,try_push_back,unchecked_emplace_back, andunchecked_push_back.

      [edit]Template parameters

      T - element type. Must beMoveConstructible andMoveAssignable.
      N - capacity, i.e. the maximum number of elements in theinplace_vector (might be0).

      [edit]Member types

      Type Definition
      value_typeT[edit]
      size_typestd::size_t[edit]
      difference_typestd::ptrdiff_t[edit]
      referencevalue_type&[edit]
      const_referenceconst value_type&[edit]
      pointervalue_type*[edit]
      const_pointerconst value_type*[edit]
      iterator implementation-definedLegacyRandomAccessIterator andrandom_access_iterator tovalue_type[edit]
      const_iterator implementation-definedLegacyRandomAccessIterator,ConstexprIterator(since C++26) andrandom_access_iterator toconst value_type[edit]
      reverse_iteratorstd::reverse_iterator<iterator>[edit]
      const_reverse_iteratorstd::reverse_iterator<const_iterator>[edit]

      [edit]Member functions

      constructs theinplace_vector
      (public member function)[edit]
      destructs theinplace_vector
      (public member function)[edit]
      assigns values to the container
      (public member function)[edit]
      assigns values to the container
      (public member function)[edit]
      assigns a range of values to the container
      (public member function)[edit]
      Element access
      access specified element with bounds checking
      (public member function)[edit]
      access specified element
      (public member function)[edit]
      access the first element
      (public member function)[edit]
      access the last element
      (public member function)[edit]
      direct access to the underlying contiguous storage
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      returns a reverse iterator to the end
      (public member function)[edit]
      Size and capacity
      checks whether the container is empty
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      [static]
      returns the maximum possible number of elements
      (public static member function)[edit]
      [static]
      returns the number of elements that can be held in currently allocated storage
      (public static member function)[edit]
      changes the number of elements stored
      (public member function)[edit]
      [static]
      reserves storage
      (public static member function)[edit]
      reduces memory usage by freeing unused memory
      (public static member function)[edit]
      Modifiers
      inserts elements
      (public member function)[edit]
      inserts a range of elements
      (public member function)[edit]
      constructs element in-place
      (public member function)[edit]
      constructs an element in-place at the end
      (public member function)[edit]
      tries to construct an element in-place at the end
      (public member function)[edit]
      unconditionally constructs an element in-place at the end
      (public member function)[edit]
      adds an element to the end
      (public member function)[edit]
      tries to add an element to the end
      (public member function)[edit]
      unconditionally adds an element to the end
      (public member function)[edit]
      removes the last element
      (public member function)[edit]
      adds a range of elements to the end
      (public member function)[edit]
      tries to add a range of elements to the end
      (public member function)[edit]
      clears the contents
      (public member function)[edit]
      erases elements
      (public member function)[edit]
      swaps the contents
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function template)[edit]
      erases all elements satisfying specific criteria
      (function template)[edit]
      lexicographically compares the values of twoinplace_vectors
      (function template)[edit]

      [edit]Notes

      The number of elements in ainplace_vector may vary dynamically up to a fixed capacity because elements are stored within the object itself similarly tostd::array. However, objects are initialized as they are inserted intoinplace_vector unlike C arrays orstd::array , which must construct all elements on instantiation.

      inplace_vector is useful in environments where dynamic memory allocations are undesired.

      Feature-test macroValueStdFeature
      __cpp_lib_inplace_vector202406L(C++26)std::inplace_vector: dynamically-resizable vector with fixed capacity inplace storage
      __cpp_lib_constexpr_inplace_vector202502L(C++26)constexprstd::inplace_vector for non-trivial element types

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <cassert>#include <inplace_vector> int main(){    std::inplace_vector<int,4> v1{0,1,2};assert(v1.max_size()==4);assert(v1.capacity()==4);assert(v1.size()==3);assert(std::ranges::equal(v1,std::array{0,1,2}));assert(v1[0]==0);assert(v1.at(0)==0);assert(v1.front()==0);assert(*v1.begin()==0);assert(v1.back()==2);    v1.push_back(3);assert(v1.back()==3);assert(std::ranges::equal(v1,std::array{0,1,2,3}));    v1.resize(3);assert(std::ranges::equal(v1,std::array{0,1,2}));assert(v1.try_push_back(3)!= nullptr);assert(v1.back()==3);assert(v1.size()==4);assert(v1.try_push_back(13)== nullptr);// no placeassert(v1.back()==3);assert(v1.size()==4);    v1.clear();assert(v1.size()==0);assert(v1.empty());}

      [edit]See also

      resizable contiguous array
      (class template)[edit]
      (C++11)
      fixed-sized inplace contiguous array
      (class template)[edit]
      double-ended queue
      (class template)[edit]

      [edit]External links

      1. inplace_vector — A reference implementation ofP0843R14 (std::inplace_vector).
      2. static_vector — Boost.Container implements inplace vector as a standalone type with its own guarantees.
      3. fixed_vector — EASTL implements inplace vector via an extra template parameter.
      4. small_vector — Folly also implements inplace vector via an extra template parameter.
      5. stack_alloc — Howard Hinnant's Custom allocators that emulatestd::inplace_vector on top ofstd::vector.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector&oldid=180892"

      [8]ページ先頭

      ©2009-2025 Movatter.jp