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.
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<vector>
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;
}
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.
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<vector>
using namespace std;
int main() {
vector<char>v = {'a', 'b', 'c', 'd', 'e'};
cout <<"Vector 'v' : ";
for (int e: v)
cout<<e<<" ";
cout<<endl;
}
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.
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<vector>
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;
}
After running this code after compilation, we have seen that the character values of vector “v” are displayed using the “while” loop.
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<vector>
#include<algorithm>
#include<iterator>
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;
}
All the vector values have been displayed on the Ubuntu shell on execution and compilation.
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.