Przejdź do głównej zawartości

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.

zanotuj

std::vector is a class template, with following type parameters, that are used within constructors:

pubTelement type
pubAllocatorAllocator type used to allocate internal storage

Default constructor

// 1)
vector()
: vector( Allocator() )
{
}

// 2)
explicit vector( Allocator const& alloc );
  • constructor 1) is noexcept if constructor of the Allocator is noexcept as well
  • constructor 2) is noexcept, because Allocator was constructed before
  • both constructors are constexpr

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();
}
Result
v.size(): 0

Repeat value constructor

// prism-push-types:size_type
constexpr vector(
size_type count,
T const& value,
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 << ' ';
}
Result (console)
15 15 15 15 

count default elements constructor

// prism-push-types:size_type
constexpr explicit vector(
size_type count,
Allocator const& alloc = Allocator()
);

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 << ' ';
}
Result (console)
0 0 0 0 13

Range-based constructor

template <typename InputIt>
constexpr vector(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);

Constructs the container with the contents of the range [first, last).

Overload resolution
This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator, to avoid ambiguity with the repeat value overload

Complexity

Given the distance between first and last as N:

  • if first and last are both forward, bidirectional or random-access iterators, the copy constructor of T is only called N times, and no reallocation occurs.
  • otherwise (first and last are just input iterators), the copy constructor of T is called O(N) times, and reallocation occurs O(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 << ' ';
}
Result (console)
H e l l o

Copy constructor

// (1)
constexpr vector( vector const& other );

// (2)
constexpr 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

The allocator is obtained as if by calling:

std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())

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 << ' ';
}
Result (console)
5 21 37 11
5 21 38 11

Move constructor

// 1)
constexpr vector( vector&& other ) noexcept;

// 2)
constexpr 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

The template parameter Allocator is only deduced from the first argument while used in class template argument deduction

.

 (od C++23)

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 << ' ';
}
Result (console)
prev.empty(): true
15 3 8

Initializer-list constructor

constexpr vector(
std::initializer_list<T> init,
Allocator const& alloc = Allocator()
);

Constructs the container with the contents of the initializer list init.

zanotuj

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 << ' ';
}
Result (console)
14 30 18
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

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.

zanotuj

std::vector is a class template, with following type parameters, that are used within constructors:

pubTelement type
pubAllocatorAllocator type used to allocate internal storage

Default constructor

// 1)
vector()
: vector( Allocator() )
{
}

// 2)
explicit vector( Allocator const& alloc );
  • constructor 1) is noexcept if constructor of the Allocator is noexcept as well
  • constructor 2) is noexcept, because Allocator was constructed before
  • both constructors are constexpr

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();
}
Result
v.size(): 0

Repeat value constructor

// prism-push-types:size_type
constexpr vector(
size_type count,
T const& value,
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 << ' ';
}
Result (console)
15 15 15 15 

count default elements constructor

// prism-push-types:size_type
constexpr explicit vector(
size_type count,
Allocator const& alloc = Allocator()
);

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 << ' ';
}
Result (console)
0 0 0 0 13

Range-based constructor

template <typename InputIt>
constexpr vector(
InputIt first,
InputIt last,
Allocator const& alloc = Allocator()
);

Constructs the container with the contents of the range [first, last).

Overload resolution
This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator, to avoid ambiguity with the repeat value overload

Complexity

Given the distance between first and last as N:

  • if first and last are both forward, bidirectional or random-access iterators, the copy constructor of T is only called N times, and no reallocation occurs.
  • otherwise (first and last are just input iterators), the copy constructor of T is called O(N) times, and reallocation occurs O(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 << ' ';
}
Result (console)
H e l l o

Copy constructor

// (1)
constexpr vector( vector const& other );

// (2)
constexpr 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

The allocator is obtained as if by calling:

std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())

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 << ' ';
}
Result (console)
5 21 37 11
5 21 38 11

Move constructor

// 1)
constexpr vector( vector&& other ) noexcept;

// 2)
constexpr 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

The template parameter Allocator is only deduced from the first argument while used in class template argument deduction

.

 (od C++23)

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 << ' ';
}
Result (console)
prev.empty(): true
15 3 8

Initializer-list constructor

constexpr vector(
std::initializer_list<T> init,
Allocator const& alloc = Allocator()
);

Constructs the container with the contents of the initializer list init.

zanotuj

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 << ' ';
}
Result (console)
14 30 18
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.