Vectors seem to be sequence containers that hold arrays that can grow or shrink in size. As a result, we don’t have to define its length when declaring it and can alter it afterward in the script. We may modify the size of std::vector flexibly while the program is executed as per our requirements, unlike std::array, whose size is set at the moment of creation and stays fixed until compilation time. We do not know the size of the array till build time in these situations (while the machine compiles the program). std::vector comes very handily here.

Syntax:

The std::vector definition syntax is similar to those of std::array, except that you shouldn’t need to give the array size together with the type of data, as can be seen here.

std::vector<datatype>array_name;

Let’s get started to get a clear understanding of std::vector in C language. Initially, we were required to open the Ubuntu 20.04 system. After that, you need to log in from it first and then open the terminal console application to start working by using Ctrl Alt T. Make sure you have some C language compiler package installed on the Ubuntu 20.04 system because, without it, we will be unable to execute our code. If you don’t have one, install the G compiler using the below command after updating your system and its apt package:

$ sudo apt update

$ sudo apt install g

Example 01:

Now your system is ready to be used. Our first example would be of empty vector, and then add-up values init one after another. Using the terminal shell, we have to create a new file to add code to it. Make sure to create a file with the “.cc” extension as representing c code. Hence, we have created a file named “main.cc” to add c code in it via the “touch” instruction as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-18.png" data-lazy- height="22" src="data:image/svg xml,” width=”337″>

By default, the file has been added to the main directory of our system. You can open it from there or directly open it via a terminal using any editor. Hence, we have used the GNU Nano editor to open it with the below-stated query.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-17.png" data-lazy- height="21" src="data:image/svg xml,” width=”341″>

Now the file is opened in the editor, paste the below-shown code in it. This code contains the input-output standard library and support for the c language. After that, we have declared a namespace in it. The main function has been initialized. We have declared an empty integer type vector name “var” with no size specified in the main method. After this, we have used the push_back() built-in method to add the values in the vector “var” one by one. Then, we have declared the “for” loop to get the vector “var” values and print them out in the shell. Press “Ctrl S” to save the code and “Ctrl X” from leaving the file and return to the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-17.png" data-lazy- height="289" src="data:image/svg xml,” width=”447″>

So, you need to compile the file code first to execute it in the shell. Hence use the g install along with the name of a file “main.cc” within a query to do so:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image5-14.png" data-lazy- height="19" src="data:image/svg xml,” width=”336″>

The compilation is successful, with zero errors. Hence, execute your file with the “a.out” query as below. The output will show you the three numbers that were pushed into vector “var”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-15.png" data-lazy- height="40" src="data:image/svg xml,” width=”327″>

Example 02:

In this example, we will initialize a size of a vector as well as its value. Hence open your main.cc file once again to update our previous code.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image7-12.png" data-lazy- height="21" src="data:image/svg xml,” width=”339″>

Now the file is opened, update your code as below. The overall code is the same except that we have initialized an integer variable “p” with 5. This variable will be used as vector size in the next lines of code. In the next consecutive line, we have used “p” as the size of the vector and value “15” as its value. This means the output must have a value of 15, 5 times in it. Again, the “for” loop is used to output the vector values in the shell. Save your code and leave the main.cc file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image6-12.png" data-lazy- height="262" src="data:image/svg xml,” width=”464″>

Compile your update file again to implement the changes using the g compiler as below. After the compilation, execute your file using the “a.out” query. You can observe it displays the value 15, five times in the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image9-10.png" data-lazy- height="56" src="data:image/svg xml,” width=”351″>

Example 03:

We can also declare a vector as we declare an array. This means we can assign values to a vector within its declaration statement. Hence, in this example, we will be discussing how to do that. So, open the “main.cc” document from the home directory using the GNU editor in the shell as presented in the below query.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image8-11.png" data-lazy- height="19" src="data:image/svg xml,” width=”334″>

After the file has been opened, paste the below-appended code into it. You can see the code is comparable to the previous one, as we haven’t changed it much. The change is only on the vector statement line. We have initialized the vector along with values in it, e.g., “100”, “200”, and “300” as an array. Then we have used the “for” loop to output these values of vector in the shell. After completing the code, save the file and navigate towards your terminal shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image12-10.png" data-lazy- height="236" src="data:image/svg xml,” width=”477″>

Compilation and execution of the file will be done by using the same previous commands. The output is given below in the picture.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image10-10.png" data-lazy- height="57" src="data:image/svg xml,” width=”337″>

Example 04:

The above example was about adding values to a vector as we do in the array. This time we will have a vector that will be initialized from some array. Open your file again within the editor for the update.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image11-10.png" data-lazy- height="19" src="data:image/svg xml,” width=”341″>

We have initialized an array “A” in the main method with 5 values in it. An integer p has been used to get the integer values from array size. The vector has been initialized while using array “A” as its value along with integer p.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image13-8.png" data-lazy- height="276" src="data:image/svg xml,” width=”476″>

The output has been shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image14-8.png" data-lazy- height="53" src="data:image/svg xml,” width=”369″>

Example 05:

You can also initialize some vector from another vector. To do this, we will update our code by opening the same file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image15-8.png" data-lazy- height="19" src="data:image/svg xml,” width=”337″>

We have declared an integer type vector var1 with some integer type values in the main function. Then we have declared another integer type vector, but we haven’t given it direct values as we usually do. We used the begin()andend” method with the name of vector 1: “var1” to add values to vector 2: “var2”. When we have printed vector 2 in the “for” loop, it works quite well.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image16-9.png" data-lazy- height="256" src="data:image/svg xml,” width=”452″>

The code works fine, as shown in the picture.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image17-7.png" data-lazy- height="55" src="data:image/svg xml,” width=”367″>

Example 06:

We will be using another method to initialize an std vector in this example. Open your file again via the shell console as below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image18-7.png" data-lazy- height="20" src="data:image/svg xml,” width=”340″>

We have declared a vector “var1” of size 100 using the “std::” statement. Initialized an integer with a value of 5. We have used the “fill” method to add values in the vector “var1”. For loop has been used again to output the values.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image19-6.png" data-lazy- height="242" src="data:image/svg xml,” width=”495″>

You can see, the output is showing that the 100 values have been displayed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image20-5.png" data-lazy- height="88" src="data:image/svg xml,” width=”720″>

Conclusion:

You have done with almost all the illustrations of vectors in the Ubuntu 20.04 system. Make sure to implement all the necessary examples to get hands-on it.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg60eb76599dabc.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.