 C++ Tutorials

# C++ Tutorials

Initialize a vector in C++ (Different ways). Difficulty Level : Easy. entering the values one-by-one – Begin. Declare v of vector type. Then we call push_back() function. This is done to insert values into vector v. Then we print “Vector elements: \n”. ” for (int a: v) print all the elements of variable a.”

1. int arr[] = {1, 3, 5, 6}; vector<int> v(arr, arr + sizeof(arr)/sizeof(arr));
2. vector<int>v; v.push_back(1); v.push_back(2); v.push_back(3); and so on.
3. vector<int>v = {1, 3, 5, 7};
``````Initializing by pushing values one by one :
Initialize Vector C++

// CPP program to create an empty vector and push values one by one Initialize Vector C++.

#include <bits/stdc++.h>
using namespace std;

int main()
{
// Create an empty vector
vector<int> vect;

vect.push_back(10);
vect.push_back(20);
vect.push_back(30);

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

return 0;
}
Output:

10 20 30
Specifying size and initializing all values Initialize Vector C++:

// CPP program to create an empty vector  and push values one by one Initialize Vector C++.
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 3;

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

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

return 0;
}
Output:

10 10 10
Initializing like arrays Initialize Vector C++:

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

int main()
{
vector<int> vect{ 10, 20, 30 };

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

return 0;
}
Output:

10 20 30
Initializing from an array Initialize Vector C++ :

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

int main()
{
int arr[] = { 10, 20, 30 };
int n = sizeof(arr) / sizeof(arr);

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

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

return 0;
}
Output:

10 20 30
Initializing from another vector Initialize Vector C++:

// CPP program to initialize a vector from  another vector.
#include <bits/stdc++.h>
using namespace std;

int main()
{
vector<int> vect1{ 10, 20, 30 };

vector<int> vect2(vect1.begin(), vect1.end());

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

return 0;
}
Output:

10 20 30
Initializing all elements with a particular value :

#include <bits/stdc++.h>
using namespace std;

int main()
{
vector<int> vect1(10);
int value = 5;
fill(vect1.begin(), vect1.end(), value);
for (int x : vect1)
cout << x << " ";
}
Output:

5 5 5 5 5 5 5 5 5 5``````

One method would be to use the array to initialize the vector

``````static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr) );``````

Now, it’s going to require you to understand initialization vectors they may be getting into those.

I’m not positive so I want to cover what that is so initialization vector is is basically like a seed value if you have a stream cipher algorithm then one value that’s going to go into it as the key and the other value that’s going to go into it is the initialization vector because if you think about it.

If you use the key to encrypt a message one the same key to encrypt a message to the same key to encrypt a message. Three then there’s going to be patterns that could be identified by the bad guy so what happens is is that you have a stream cipher using the key. As the instructions and then you have this initialization vector which should it should be different for each message the initialization vector provides randomness to the process within a stream cipher so you have key.

Key one used to encrypt your message and you have initialization vector one then the next message you could have key one and of initialization vector two. So it’s just like a seed value that’s that’s used by the symmetric stream cipher to add more randomness so there are not any patterns in the resulting ciphertext.

The initializer list is an alternative way for initializing the members of the new object here in this code sample. We can find this initializer list that refers to with and I two members that exist in each and every new object instantiated from the class rectangle. This initializer list will be executed when we try when we instantiate rectangle using this zero-argument constructor here in this code sample we can see that the area printer to the screen is 100 which fits the values the initializer list assign to width and height.

## Initialize 2d vector c++

``````Use the std::vector::vector(count, value) constructor that accepts an initial size and a default value:

std::vector<std::vector<int> > fog(
A_NUMBER,
std::vector<int>(OTHER_NUMBER)); // Defaults to zero initial value
If a value other than zero, say 4 for example, was required to be the default then:

std::vector<std::vector<int> > fog(
A_NUMBER,
std::vector<int>(OTHER_NUMBER, 4));
I should also mention uniform initialization was introduced in C++11, which permits the initialization of vector, and other containers, using {}:

std::vector<std::vector<int> > fog { { 1, 1, 1 },
{ 2, 2, 2 } };``````

## Initialize vector c++ with 0

You don’t need initialization lists for that:

``````std::vector<int> vector1(length, 0);
std::vector<double> vector2(length, 0.0);``````

## Initialize vector c++ with size

``````std::vector<Entry> phone_book;
for (std::size_t i = 0; i < n; ++i)
{
phone_book[i] = entry; // <-- !! Undefined Behaviour !!
}``````

## Initialize empty vector c++

Both `std::string` and `std::vector<T>` have constructors initializing the object to be empty. You could use `std::vector<unsigned char>()` but I’d remove the initializer.

## Initialize vector of vector c++

In the case of a normal vector we initialize it as: 1. vector<datatype> variable_name. Now in the case of a 2D vector all we do is create.

This doesn’t use `boost::assign` but does what you need:

``````vector<vector<int>> v(10, vector<int>(10,1));
``````

This creates a vector containing 10 vectors of `int`, each containing 10 `ints`.

## How to return a vector in c++

In C++11, this is the preferred way:

``````std::vector<X> f();
``````

That is, return by value.

With C++11, `std::vector` has move-semantics, which means the local vector declared in your function will be moved on return and in some cases even the move can be elided by the compiler.

## Initialize vector after declaration c++

You simply need to resize the vector before adding the new values:

``v1.resize(20);``