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 vecOfInts;

Initializing by pushing values one by one :

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

int main() 
{ 
	// Make an empty vector 
	vector 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  
using namespace std; 

int main() 
{ 
	int n = 3; 

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

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

	return 0; 
} 

Initializing like arrays :

// c++ Example to initialize a vector like 
// an array. 
#include  
using namespace std; 

int main() 
{ 
	vector 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  
using namespace std; 

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

	vector 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  
using namespace std; 

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

	vector myvct2(myvct1.begin(), myvct1.end()); 

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

	return 0; 
} 

std::vector in C++

Length of std::vector

#include 
#include 

int main()
{
    std::vector 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 
#include 

using namespace std;

void printVector(const std::vector &n)
{

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

}

int main()
{

    vector 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 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

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

back

#include 
#include 

using namespace std;

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

empty

#include 
#include 

using namespace std;

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

resize

#include 
#include 

using namespace std;

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

max_size

#include 
#include 

using namespace std;

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

assign

#include 
#include 

using namespace std;

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

push_back

#include 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

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

insert

#include 
#include 
#include 

using namespace std;

void printVector(const std::vector &n)
{

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

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

}

int main()
{
    vector 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 myvct1 = {555,555};

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

    return 0;
}

erase

#include 
#include 
#include 

using namespace std;

int main()
{
    vector myvct1 = {14, 15, 16, 17, 18};
    vector 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 
#include 

using namespace std;

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

    myvct1.clear();

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

    return 0;
}

swap

#include 
#include 

using namespace std;

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

    vector 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 
#include 
using namespace std;

int main()
{
    vector>  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 
#include 
using namespace std;

void display(const std::vector> &v)
{
    for(int i=0; i>  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.

Leave a Comment