std::inplace_vector<T,N>::inplace_vector
From cppreference.com
< cpp | container | inplace vector
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::inplace_vector
inplace_vector::inplace_vector
constexpr inplace_vector() noexcept;
(1)
(since C++26)
constexpr explicit inplace_vector( size_type count );
(2)
(since C++26)
constexpr inplace_vector( size_type count, const T& value );
(3)
(since C++26)
template< class InputIt >
constexpr inplace_vector( InputIt first, InputIt last );
(4)
(since C++26)
constexpr inplace_vector( InputIt first, InputIt last );
template< container-compatible-range <T> R >
constexpr inplace_vector( std::from_range_t, R&& rg );
(5)
(since C++26)
constexpr inplace_vector( std::from_range_t, R&& rg );
constexpr inplace_vector( const inplace_vector& other );
(6)
(since C++26)
constexpr inplace_vector( inplace_vector&& other )
noexcept(N == 0 || std::is_nothrow_move_constructible_v <T>);
(7)
(since C++26)
noexcept(N == 0 || std::is_nothrow_move_constructible_v <T>);
constexpr inplace_vector( std::initializer_list <T> init );
(8)
(since C++26)
Constructs a new inplace_vector
from a variety of data sources.
1) Constructs an empty
inplace_vector
whose data() == nullptr and size() == 0.2) Constructs an
inplace_vector
with count default-inserted elements.3) Constructs an
inplace_vector
with count copies of elements with value value.4) Constructs an
inplace_vector
with the contents of the range [
first,
last)
.5) Constructs an
inplace_vector
with the contents of the range rg. The constructor is a trivial copy constructor if N > 0 and std::is_trivially_copy_constructible_v <T> are both true.
7) A move constructor. Constructs an
inplace_vector
with the contents of other using move semantics. The constructor is a trivial move constructor if N > 0 and std::is_trivially_move_constructible_v <T> are both true.
8) Constructs an
inplace_vector
with the contents of the initializer list init.[edit] Parameters
count
-
the size of the container
value
-
the value to initialize elements of the container with
first, last
-
the pair of iterators defining the source range of elements to copy
rg
-
the range of values to initialize elements of the container with
other
-
another
inplace_vector
to be used as source to initialize the elements of the container with
init
-
initializer list to initialize the elements of the container with
Type requirements
-
T
must meet the requirements of DefaultInsertable in order to use overloads (2,3).
[edit] Complexity
1) Constant.
2,3) Linear in count.
4) Linear in std::distance (first, last).
5) Linear in std::ranges::distance (rg).
6,7) Linear in size of other.
8) Linear in size of init.
[edit] Exceptions
[edit] Example
Run this code
#include <cassert> #include <initializer_list> #include <inplace_vector> #include <new> #include <print> #include <ranges> int main() { std::inplace_vector <int, 4> v1; // overload (1) assert (v1.size() == 0 && v1.capacity() == 4); std::inplace_vector <int, 0> v2; // overload (1), N == 0 is allowed assert (v2.size() == 0 && v2.capacity() == 0); std::inplace_vector <int, 5> v3(3); // overload (2) assert (v3.size() == 3 && v3.capacity() == 5); std::println ("v3 = {}", v3); try { std::inplace_vector <int, 3> v(4); // overload (2), throws: count > N } catch(const std::bad_alloc & ex1) { std::println ("ex1.what(): {}", ex1.what()); } std::inplace_vector <int, 5> v4(3, 8); // overload (3) assert (v4.size() == 3 && v4.capacity() == 5); std::println ("v4 = {}", v4); try { std::inplace_vector <int, 3> v(4, 2); // overload (3), throws: count > N } catch(const std::bad_alloc & ex2) { std::println ("ex2.what(): {}", ex2.what()); } const auto init = {1, 2, 3}; std::inplace_vector <int, 4> v5(init.begin(), init.end()); // overload (4) assert (v5.size() == 3 && v5.capacity() == 4); std::println ("v5 = {}", v5); std::inplace_vector <int, 4> v6(std::from_range, init); // overload (5) assert (v6.size() == 3 && v6.capacity() == 4); std::println ("v6 = {}", v6); std::inplace_vector <int, 4> v7(v6); // overload (6) assert (v7.size() == 3 && v7.capacity() == 4); std::println ("v7 = {}", v7); assert (v6.size() == 3); std::inplace_vector <int, 4> v8(std::move(v6)); // overload (7) // Note that after the move v6 is left in valid but indeterminate state. assert (v8.size() == 3 && v8.capacity() == 4); std::println ("v8 = {}", v8); std::inplace_vector <int, 4> v9(init); // overload (8) assert (v9.size() == 3 && v9.capacity() == 4); std::println ("v9 = {}", v9); try { std::inplace_vector <int, 2> v(init); // overload (8), throws: init.size() > N } catch(const std::bad_alloc & ex3) { std::println ("ex3.what(): {}", ex3.what()); } }
Possible output:
v3 = [0, 0, 0] ex1.what(): std::bad_alloc v4 = [42, 42, 42] ex2.what(): std::bad_alloc v5 = [1, 2, 3] v6 = [1, 2, 3] v7 = [1, 2, 3] v8 = [1, 2, 3] v9 = [1, 2, 3] ex3.what(): std::bad_alloc
[edit] See also
[static]
(public static member function) [edit]