std::vector constructors
std::vector class can be constructed in many different ways.
Use the button in the top-right corner to navigate with arrows for convenience.
std::vector is a class template, with following type parameters,
that are used within constructors:
| pub | T | element type |
| pub | Allocator | Allocator type used to allocate internal storage |
Default constructor
- C++20
- C++17
- do C++17
- Simplified
- Detailed
// 1)
vector()
: vector( Allocator() )
{
}
// 2)
explicit vector( Allocator const& alloc );
- constructor 1) is
noexceptif constructor of theAllocatorisnoexceptas well - constructor 2) is
noexcept, becauseAllocatorwas constructed before - both constructors are
constexpr
// 1)
constexpr vector() noexcept(noexcept( Allocator() ))
: vector( Allocator() )
{
}
// 2)
explicit constexpr vector( Allocator const& alloc ) noexcept;
- Simplified
- Detailed
// 1)
vector()
: vector( Allocator() )
{
}
// 2)
explicit vector( Allocator const& alloc );
- constructor 1) is
noexceptif constructor of theAllocatorisnoexceptas well - constructor 2) is
noexcept, becauseAllocatorwas constructed before
// 1)
vector() noexcept(noexcept( Allocator() ))
: vector( Allocator() )
{
}
// 2)
explicit vector( Allocator const& alloc ) noexcept;
vector();
explicit vector( Allocator const& alloc );
Default constructor. Constructs an empty container. If no allocator is supplied, allocator is obtained from a default-constructed instance.
Complexity
Constant.
Example
#include <iostream>
#include <vector>
int main() {
// Default constructed vector<int>:
std::vector<int> v;
std::cout << "v.size(): " << v.size();
}
v.size(): 0
Repeat value constructor
- C++20
- do C++20
- do C++11
constexpr vector(
size_type count,
T const& value,
Allocator const& alloc = Allocator()
);
vector(
size_type count,
T const& value,
Allocator const& alloc = Allocator()
);
vector(
size_type count,
T const& value = T(),
Allocator const& alloc = Allocator()
);
Constructs the vector with count copies of the value.
Complexity
Linear in count.
Example
#include <iostream>
#include <vector>
int main() {
std::vector<int> v(4, 15);
for (auto elem : v)
std::cout << elem << ' ';
}
15 15 15 15
count default elements constructor
- C++20
- od C++14
- od C++11
constexpr explicit vector(
size_type count,
Allocator const& alloc = Allocator()
);
explicit vector(
size_type count,
Allocator const& alloc = Allocator()
);
explicit vector( size_type count );
Constructs the vector with count default-inserted elements of type T. No copies are made.
Complexity
Linear in count.
Example
#include <iostream>
#include <vector>
int main() {
// |0,0,0,0|
auto vec = std::vector<int>(4);
// |0,0,0,0,13|
vec.push_back(13);
for (auto elem : vec)
std::cout << elem << ' ';
}
0 0 0 0 13
Range-based constructor
- C++20
- do C++20
template <typename InputIt>
constexpr vector(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);
template <typename InputIt>
vector(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);
Constructs the container with the contents of the range [first, last).
Overload resolution
Complexity
Given the distance between first and last as N:
- if
firstandlastare both forward, bidirectional or random-access iterators, the copy constructor ofTis only calledNtimes, and no reallocation occurs. - otherwise (
firstandlastare just input iterators), the copy constructor ofTis calledO(N)times, and reallocation occursO(log N)times.
Example
#include <iostream>
#include <vector>
int main() {
char buf[] = "Hello, World";
auto vec = std::vector<char>(
std::begin(buf),
std::next(std::begin(buf), 5)
);
for (auto elem : vec)
std::cout << elem << ' ';
}
H e l l o
Copy constructor
- C++20
- do C++20
// (1)
constexpr vector( vector const& other );
// (2)
constexpr vector(
vector const& other,
Allocator const& alloc
);
// (1)
vector( vector const& other );
// (2)
vector(
vector const& other,
Allocator const& alloc
);
Copy constructor. Constructs the container with the copy of the contents of other.
The second version uses alloc as the allocator.
Obtaining the allocator and deduction
Complexity
Linear in size of other.
Example
#include <iostream>
#include <vector>
int main() {
// ⚠ using C++17 CTAD
auto vec = std::vector{5, 21, 37, 11};
auto copied = std::vector(vec);
copied[2] = 38;
for (auto elem: vec) std::cout << elem << ' ';
std::cout << '\n';
for (auto elem: copied) std::cout << elem << ' ';
}
5 21 37 11
5 21 38 11
Move constructor
- C++20
- od C++17
- od C++11
// 1)
constexpr vector( vector&& other ) noexcept;
// 2)
constexpr vector(
vector&& other,
Allocator const& alloc
);
// 1)
vector( vector&& other ) noexcept;
// 2)
vector(
vector&& other,
Allocator const& alloc
);
// 1)
vector( vector&& other );
// 2)
vector(
vector&& other,
Allocator const& alloc
);
Move constructor. Constructs the container with the contents of other using move semantics.
Allocator is obtained by move-construction from the allocator belonging to other.
After the move, other is guaranteed to be empty().
The second overload accepts an allocator that is used for the constructed container, moving the contents from other.
If alloc != other.get_allocator(), this results in an element-wise move (in that case, other is not guaranteed to be empty() after the move).
Allocator deduction
Complexity
Linear if alloc != other.get_allocator(), otherwise constant.
Example
#include <iostream>
#include <vector>
#include <iomanip>
int main() {
auto prev = std::vector{15, 3, 8};
auto current = std::vector( std::move(prev) );
std::cout << "prev.empty(): " << std::boolalpha << prev.empty() << '\n';
std::cout << "current: ";
for (auto elem: current)
std::cout << elem << ' ';
}
prev.empty(): true
15 3 8
Initializer-list constructor
- C++20
- od C++11
constexpr vector(
std::initializer_list<T> init,
Allocator const& alloc = Allocator()
);
vector(
std::initializer_list<T> init,
Allocator const& alloc = Allocator()
);
Constructs the container with the contents of the initializer list init.
Presence of this overload means list initialization and direct initialization do different things:
std::vector<int> b{3}; // creates a 1-element vector holding {3}
std::vector<int> a(3); // creates a 3-element vector holding {0, 0, 0}
std::vector<int> d{1, 2}; // creates a 2-element vector holding {1, 2}
std::vector<int> c(1, 2); // creates a 1-element vector holding {2}
Complexity
Linear in size of init.
Example
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = { 14, 30, 18 };
for (auto elem : v)
std::cout << elem << ' ';
}
14 30 18
Hover to see the original license.