Vectors are identical to fluid arrays, except they can resize. Vectors are sequential units that could grow or shrink in size when items are added or removed. Containers are entities that store information of the same kind. Vectors may set aside some additional storage for future development of the vector’s components.

Adjacent memory is used to stock vector elements. Therefore, we have decided to write this article for those naïve users who don’t know how to display vectors on the shell using C .

Let’s get started with the terminal shell opening via the shortcut “Ctrl Alt t”. You must have the Nano editor, and G compiler of C configured on your Linux system as we have been working on Ubuntu 20.04.

Before starting our examples, we will be creating a new simple C file and opening it with a nano editor. Both the commands are shown beneath.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/1-19.jpg" data-lazy- height="38" src="data:image/svg xml,” width=”401″>

Example 01: Using “For” Loop

Let’s get started with the first example of displaying or printing the vector data structure in the Ubuntu 20.04 shell while working in the C language. Start your code with the addition of some main headers of C . The first one is standard “iostream” to utilize the input and output stream. The other header library must be “vector” to utilize the vector data structures in our code. The “std” namespace for the C language must be added to use the standard “cin” and “cout” statements in the script.

The main() function comes after the standard namespace. It started with initializing an integer type vector “v” taking 5 integer values in it. This vector is resizable. The cout standard clause is here to tell us that the vector will be displayed. The “for” loop is started from the 1st index of the vector up to its end using the “size” function.

The cout clause is utilizing the “at()” function to iterate the vector values using indexes i.e. “i” and print all values of the vector “v”.

#include

#include

using namespace std;

int main() {

   vector<int>v = {12,14,16,18,20};

   cout <<“Vector ‘v’ : “;

   for(int i=0; i <v.size(); i ) {

       cout <<a.at(i) <<‘ ‘; }

   cout<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/2-19.jpg" data-lazy- height="198" src="data:image/svg xml,” width=”437″>

Save this code with “Ctrl S” and quit this C file with “Ctrl X” to come out of the editor. As we have returned to the shell, it’s time to utilize the “G ” compiler to compile our newly made code.

Use the file name along with the keyword “g ”. The compilation will be seen as successful if it doesn’t show any output. There comes the “./a.out” instruction of Ubuntu 20.04 to execute the compiled code.

Using both the commands in our Linux system leads us to the output showing the vector elements on the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/3-17.jpg" data-lazy- height="52" src="data:image/svg xml,” width=”335″>

Example 02: Using For Loop with “each” Element

Let’s take a look at the new example to use the “for” loop in a different way. This time, we will be taking the same code with minor changes. The very first change we have been doing is at the vector initialization line.

We have changed the whole vector along with its type. We used the character type vector “v” with 5 character values, i.e., alphabets. The other change has been done to the “for” loop. We have initialized an “each” element as “e” taking the vector “v” as a source to get elements one after another.

Each element “e” will be displayed using the “cout” statement. After this “for” loop ends, we have given a line break, and the code is completed.

#include

#include

using namespace std;

int main() {

   vector<char>v = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};

   cout <<“Vector ‘v’ : “;

   for (int e: v)

       cout<<e<<” “;

   cout<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/4-16.jpg" data-lazy- height="203" src="data:image/svg xml,” width=”469″>

This code got compiled using the same “g ” compiler of Ubuntu 20.04 for C . On running this compiled code on the shell, we have got the result as numbers. This implies that the “for” loop will always convert a string or character values of a vector to numbers before displaying.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/5-15.jpg" data-lazy- height="54" src="data:image/svg xml,” width=”352″>

Example 03:

Let’s see how the “while” loop will work on the vectors when used. Thus, we have been using the overall same code once again. The first change is initializing an integer “i” to 0. The same character-type vector is used.

Until the value “i” is less than the size of a vector, the cout statement within the “While” loop will continue to display the particular index value of the vector and increment “i” by 1. Let’s compile this code with g to see the results.

#include

#include

using namespace std;

int main() {

   vector<char>v = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};

   cout <<“Vector ‘v’ : “;

   while (int i <v.size()) {

       cout<<v[i]<<” “;

       i ; }

   cout<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/6-15.jpg" data-lazy- height="254" src="data:image/svg xml,” width=”468″>

After running this code after compilation, we have seen that the character values of vector “v” are displayed using the “while” loop.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/7-11.jpg" data-lazy- height="53" src="data:image/svg xml,” width=”359″>

Example 04:

Let’s look at the last example to use the copy function and the iterator to display the contents/values of a vector. Firstly, to use the iterator and the copy() function, you must add the algorithm and iterator header after the iostream and vector library using “#include”.

The integer vector “v” is initialized and the copy() function is started with “begin()” and “end()” functions to take the start and end of the vector. The ostream_iterator is here to iterate the vector values and it is utilizing the “cout” statement to display all values.

#include

#include

#include

#include

using namespace std;

int main() {

   vector<int>v = {12,14,16,18,20};

   cout <<“Vector ‘v’ : “;

   copy(v.begin(), v.end(), ostream_iterator<int>(cout, ” “));

   cout<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/8-9.jpg" data-lazy- height="218" src="data:image/svg xml,” width=”555″>

All the vector values have been displayed on the Ubuntu shell on execution and compilation.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/9-7.jpg" data-lazy- height="53" src="data:image/svg xml,” width=”337″>

Conclusion:

This was all about initializing and printing an iterator in C code using the Ubuntu 20.04 system. We have adopted a total of 4 different methods to get similar results, i.e., for loop, for each loop, while loop, copy function, and the iterator. You can make use of these examples in any of the C environments.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content