In this article we will discuss different ways to initialize a vector in C++.
Creating a vector object without any initialization will create an empty vector with no elements i.e.
std::vector<int> vecOfInts;
But we generally want to initialize a vector with huge values and calling push_back() that many times to add element in vector is not an efficient and intelligent solution. So, let’s discuss how to initialize a vector in different ways,
Vector provides a constructor that accepts the size as an argument and initialize the vector with that many objects of default value i.e.
// Initialize vector with 5 integers // Default value of all 5 ints will be 0. std::vector<int> vecOfInts(5); for(int x : vecOfInts) std::cout<<x<<std::endl;
Output:
0 0 0 0 0
Many times we want to initialize a vector with an element of particular value instead of default value. For that vector provides an overloaded constructor i.e.
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
It accepts the size of vector and an element as an argument. Then it initializes the vector with n elements of value val.
Lets see an example that how to initialize a vector of std::string to 5 string objects with value “Hi”.
// Initialize vector to 5 string objects with value "Hi" std::vector<std::string> vecOfStr(5, "Hi"); for(std::string str : vecOfStr) std::cout<<str<<std::endl;
Output:
Hi Hi Hi Hi Hi
In above two examples we saw how to initialize a vector with same kind of value either default value or a particular value. But what if we want to initialize a vector with an array of elements. For that vector provides an over loaded constructor i.e.
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
It accepts a range as an argument i.e. two iterators and initializes the vector with elements in range (first, last] i.e. from first till last -1.
[showads ad=inside_post]
We will use the same overloaded constructor to initialize a vector of string from an array of strings i.e.
// Create an array of string objects std::string arr[] = {"first", "sec", "third", "fourth"}; // Initialize vector with a string array std::vector<std::string> vecOfStr(arr, arr + sizeof(arr)/sizeof(std::string)); for(std::string str : vecOfStr) std::cout<<str<<std::endl;
Output:
first sec third fourth
We will use the same overloaded constructor of std::vector to initialize a vector with range i.e.
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
This time range will be of std::list’s iterator i.e.
// Create an std::list of 5 string objects std::list<std::string> listOfStr; listOfStr.push_back("first"); listOfStr.push_back("sec"); listOfStr.push_back("third"); listOfStr.push_back("fouth"); // Initialize a vector with std::list std::vector<std::string> vecOfStr(listOfStr.begin(), listOfStr.end()); for(std::string str : vecOfStr) std::cout<<str<<std::endl;
Output:
first sec third fourth
Vector provides a constructor that receives other vector as an argument and initializes the current vector with the copy of all elements of provided vector i.e.
vector (const vector& x);
Lets how to initialize a vector of string with another vector of same type i.e.
std::vector<std::string> vecOfStr; vecOfStr.push_back("first"); vecOfStr.push_back("sec"); vecOfStr.push_back("third"); // Initialize a vector with other string object std::vector<std::string> vecOfStr3(vecOfStr);
Complete code with all 5 different ways to initialize a vector is as follows,
#include <iostream> #include <vector> #include <iterator> #include <list> #include <string> void example1() { // Initialize vector with 5 integers // Default value of all 5 ints will be 0. std::vector<int> vecOfInts(5); for (int x : vecOfInts) std::cout << x << std::endl; } void example2() { // Initialize vector to 5 string objects with value "Hi" std::vector<std::string> vecOfStr(5, "Hi"); for (std::string str : vecOfStr) std::cout << str << std::endl; } void example3() { // Create an array of string objects std::string arr[] = { "first", "sec", "third", "fourth" }; // Initialize vector with a string array std::vector<std::string> vecOfStr(arr, arr + sizeof(arr) / sizeof(std::string)); for (std::string str : vecOfStr) std::cout << str << std::endl; } void example4() { // Create an std::list of 5 string objects std::list<std::string> listOfStr; listOfStr.push_back("first"); listOfStr.push_back("sec"); listOfStr.push_back("third"); listOfStr.push_back("fouth"); // Initialize a vector with std::list std::vector<std::string> vecOfStr(listOfStr.begin(), listOfStr.end()); for (std::string str : vecOfStr) std::cout << str << std::endl; // Initialize a vector with other string object std::vector<std::string> vecOfStr3(vecOfStr); for (std::string str : vecOfStr3) std::cout << str << std::endl; } int main() { example1(); example2(); example3(); example4(); return 0; }