how to initialize a vector in c++ ?

In initialize vector c++, A vector is a dynamic array class executed in the default C++ library. It vector ​class has the capacity to dynamically expand with reduce. The component are placed in a adjacent rage therefor that they can be traversed by iterators.

The very first step imp in using vectors is to know how to declare as well as initialize them; there are 5+ several ways of initializing a vector in C++ Examples:

initialize vector c++

Also You can learn std::vector in C++, What is std::vector?, Declaration of std::vector, Initialization of std::vector, Length of std::vector, Passing std::vector to function, Multidimensional std::vector, Passing a multidimensional std::vector to a function or many more Member Functions.

c++ vector, vector c++, std::vector, define vector, vectors c++, c vector, c++ vectors, initialize vector c++, vectors in c++, std vector, vector size c++, push_back c++, vector in c++, c++ vector size, vector push_back, vector size, cpp vector, c++ length of array, vector c, c++ vector example, vector functions c++
initialize_vector_cpp

There are the following the Initialize a define vector in C++ as shown below:(5 several ways)
Initialization vector can be done in several ways with examples of vectors

  • Initializing by pushing values one by one
  • Specifying size and initializing all values
  • Initializing like arrays
  • Initializing from an array
  • Initializing from another vector

vector c++ & c++ vector initialization

Creating a vector object without any types of the initialization will make an empty vector with no any elements example like.

std::vector<int> vecOfInts;

Initializing by pushing values one by one :

// c++ Example to make an empty vector 
// with push values step by step. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Make an empty vector 
	vector<int> myvct; 
	
	myvct.push_back(40); 
	myvct.push_back(50); 
	myvct.push_back(60); 

	for (int x : myvct) 
		cout << x << " "; 

	return 0; 
} 

Specifying size and initializing all values :

// c++ Example to make an empty vector 
// with push values step by step. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	int n = 3; 

	// Create a vector of size n with 
	// all values as 40. 
	vector<int> myvct(n, 40); 

	for (int x : myvct) 
		cout << x << " "; 

	return 0; 
} 

Initializing like arrays :

// c++ Example to initialize a vector like 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	vector<int> myvct{ 40, 50, 60 }; 

	for (int x : myvct) 
		cout << x << " "; 

	return 0; 
} 

Initializing from an array :

// c++ Example to initialize a vector from 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	int arr[] = { 40, 50, 60 }; 
	int n = sizeof(arr) / sizeof(arr[0]); 

	vector<int> myvct(arr, arr + n); 

	for (int x : myvct) 
		cout << x << " "; 

	return 0; 
} 

Initializing from another vector :

// c++ Example to initialize a vector from 
// second vector. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	vector<int> myvct1{ 40, 50, 60 }; 

	vector<int> myvct2(myvct1.begin(), myvct1.end()); 

	for (int x : myvct2) 
		cout << x << " "; 

	return 0; 
} 

std::vector in C++

Length of std::vector

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> ages = {41, 98, 27, 14, 58};
    ages = {45, 35, 15};
    std::cout << "length of array : " << ages.size() << std::endl;
    return 0;
}

Passing std::vector to function

#include <iostream>
#include <vector>

using namespace std;

void printVector(const std::vector<int> &n)
{

    for (int j = 0; j < n.size(); j++ )
    {
        cout << "n[" << j << "] = " << n[j] << endl;
    }

}

int main()
{

    vector<int> n = {1,2,3,4,5};

    printVector(n);

    return 0;
}

Member Functions

There are the following the Member Functions several ways Like as front, back, empty, resize, max_size, assign, push_back, pop_back, capacity, reserve, insert, erase, clear, c++ push_back, vector push_back, push_back c++ and swap.

at

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> ages = {41, 98, 27, 14, 58};
    ages.at(2) = 72;
    for(int i = 0; i < ages.size() ; i++)
    {
        cout << "ages[" << i << "] = " << ages.at(i) << endl;
    }
    return 0;
}

front

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> ages = {41, 98, 27, 14, 58};
    cout << ages.front() << endl;
    return 0;
}

back

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> ages = {41, 98, 27, 14, 58};
    cout << ages.back() << endl;
    return 0;
}

empty

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1 = {5, 6};
    vector<int> myvct2;
    cout << myvct1.empty() << endl;
    cout << myvct2.empty() << endl;
    return 0;
}

resize

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1 = {5, 6};
    myvct1.resize(5);
    for(int i = 0; i < myvct1.size() ; i++)
    {
        cout << myvct1[i] << endl;
    }
    return 0;
}

max_size

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1 = {11, 12, 13, 14, 15};
    cout << myvct1.size() << endl;
    cout << myvct1.max_size() << endl;
    return 0;
}

assign

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct;
    myvct.assign(7, 40);  // 7 elements each of value 40
    cout << myvct.size() << endl;
    return 0;
}

push_back

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct = {14, 15, 16, 17, 18};
    myvct.push_back(47);

    //printing values of myvct
    cout << "elements of myvct" << endl;
    for(int i = 0; i < myvct.size(); i++)
    {
        cout << myvct[i] << endl;
    }

    return 0;
}

pop_back

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct = {14, 15, 16, 17, 18};
    myvct.pop_back();

    //printing values of myvct
    cout << "elements of myvct" << endl;
    for(int i = 0; i < myvct.size(); i++)
    {
        cout << myvct[i] << endl;
    }

    return 0;
}

capacity

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1;
    for(int i = 0; i < 50; i++)
    {
        myvct1.push_back(1);
    }
    cout << "size : " << myvct1.size() << endl;
    cout << "max_size : " << myvct1.max_size() << endl;
    cout << "capacity : " << myvct1.capacity() << endl;
    return 0;
}

reserve

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1;
    for(int i = 0; i < 50; i++)
    {
        myvct1.push_back(1);
    }
    myvct1.reserve(100);
    cout << "capacity : " << myvct1.capacity() << endl;
    return 0;
}

insert

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

void printVector(const std::vector<int> &n)
{

    cout << "Vector is :" << endl;

    for (auto i: n)
    {
        std::cout << ' ' << i;
    }
    std::cout << '\n';

}

int main()
{
    vector<int> myvct = {111,222};
    printVector(myvct);

    auto it = myvct.begin();

    myvct.insert(it,000);
    printVector(myvct);

    it = myvct.begin();
    myvct.insert(it,3,333);
    printVector(myvct);

    vector<int> myvct1 = {555,555};

    it = myvct.begin();
    myvct.insert(it+4, myvct1.begin(), myvct1.end());
    printVector(myvct);

    return 0;
}

erase

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    vector<int> myvct1 = {14, 15, 16, 17, 18};
    vector<int> myvct2 = {11, 12, 13, 14, 15};

    myvct1.erase(myvct1.begin()+4);  // removing a single element at position 4
    myvct2.erase(myvct2.begin()+1, myvct2.begin()+3);   // removing range of elements from position 1 till 2

    //printing the values of myvct1
    cout << "Values of myvct1" << endl;
    for(int i = 0; i < myvct1.size(); i++)
    {
        cout << myvct1[i] << endl;
    }

    //printing the values of myvct2
    cout << "Values of myvct2" << endl;
    for(int i = 0; i < myvct2.size(); i++)
    {
        cout << myvct2[i] << endl;
    }
    return 0;
}

clear

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1 = {14, 15, 16, 17, 18};

    myvct1.clear();

    for (auto i: myvct1)
    {
        std::cout << i << endl;
    }

    return 0;
}

swap

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> myvct1 = {1, 2, 3};

    vector<int> myvct2 = {4, 5, 6};

    myvct1.swap(myvct2);

    std::cout << "Vector myvct1" << endl;
    for (auto i: myvct1)
    {
        std::cout << i << endl;
    }

    std::cout << "Vector myvct2" << endl;
    for (auto i: myvct2)
    {
        std::cout << i << endl;
    }

    return 0;
}

Multidimensional std::vector

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>>  v {{{1,2,3},{4,5,6},{7,8,9}}};
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            cout << v[i][j] << "\t";
        }
        cout << endl;
    }
    return 0;
}

Passing a multidimensional std::vector to a function

#include <iostream>
#include <vector>
using namespace std;

void display(const std::vector<std::vector<int>> &v)
{
    for(int i=0; i<v.size(); i++)
    {
        for(int j = 0; j<v[i].size(); j++)
        {
            cout << v[i][j] << "\t";
        }
        cout << endl;
    }
}

int main()
{
    vector<vector<int>>  v {{{1,2,3},{4,5,6},{7,8,9}}};
    display(v);
    return 0;
}

See also

vector::operator= Assign content (public member function)
vector::assign Assign vector content (public member function)
vector::reserve Request a change in capacity (public member function)
vector::resize Change size (public member function)
vector::clear Clear content (public member function)
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about initialize vector c++.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to Open URL in New Tab using c#
  2.   how to convert string to char c++ ?
  3.   C# Named Arguments Tutorial with Examples
  4.   C# nested while loops and multiple conditions
  5.   Basic Computer Programming Languages Principles
  6.   C# Hash table and Dictionary Tutorial with Examples
  7.   Access Control Allow Origin http set Headers
  8.   Linear Search in C, C++, JAVA, PHP, Python3 and C#
  9.   Star Pyramid and patterns in C programming
  10.   Simple C Programming Structure Examples