Broad Network


Vector Element Access in C++

Container Library Sequences in C++ Simplified – Part 5

Forward: In this part of the series, we see how to access elements in a vector.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is the part 5 of my series, Containers Library Sequences in C++, Simplified. In this part of the series, we see how to access elements in a vector. I assume that you have read the previous parts of this series. There are 8 access commands that we shall look at here. I introduce each command with its syntax. Do not get frightened when you just see syntax, because I will explain.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

reference operator[](size_type n);
We are talking here about the [] operator. It takes an integer as argument. This is the list index, whose counting begins from zero. Consider size_type here as an integer. The operator returns a reference, which is equivalent to the object type of the element; you should know the object type.

The following code illustrates the use of the operator.

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);
        int myIn = myVtor[3];
        cout << myIn;

        return 0;
    }

Read and try the above code. In place of a reference, I have used an int because I knew that T was an int, that is, the chosen element type for all the elements in the program was an int. To use the operator, you begin with the instantiated vector name. This is followed by the square brackets. Inside the square brackets you have the index of the element in the vector list.

const_reference operator[](size_type n) const;
This is similar to the previous syntax but for the reserved word, const, at the end. Because of this word, the value returned cannot be modified. Note that the returned type is const_reference and not just reference. The following code illustrates the use of the syntax.

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);
        const int myIn = myVtor[3];
        cout << myIn;

        return 0;
    }

Note that the object receiving the returned value is a constant, that is, “const int myIn”.

reference at(size_type n);
This is a function. It is similar to “reference operator[](size_type n);” but the argument n has to be valid. If it is not valid, an exception will be thrown by the system. Note that exceptions that are not caught can lead to your program aborting. The following code illustrates the use of the syntax:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);
        int myIn = myVtor.at(3);
        cout << myIn;

        return 0;
    }

const_reference at(size_type n) const;
Similar to “reference at(size_type n);” but the value returned cannot be changed. Example:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);
        const int myIn = myVtor.at(3);
        cout << myIn;

        return 0;
    }

Note the statement of the returned value.

Assigning Values to Elements
You assign a value to an element in the same way that you do for an array. The following code illustrates this:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);

        myVtor[0] = 0;    
        myVtor[1] = 10;      
        myVtor[2] = 20;      
        myVtor[3] = 30;      
        myVtor[4] = 40;          

        cout << myVtor[0] << "\n";
        cout << myVtor[1] << "\n";
        cout << myVtor[2] << "\n";
        cout << myVtor[3] << "\n";
        cout << myVtor[4] << "\n";

        return 0;
    }

To assign a value to an element, type the instantiated vector name (vector object name) and then type the square brackets. In the square brackets, type the index. Then you have the assignment operator and value. Any previous value for the element is replaced.

reference front();
This is a method that returns the reference to the first element of the vector list. The reference is equivalent to value of the first element. Read and try the following code:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);

        myVtor[0] = 0;    
        myVtor[1] = 10;      
        myVtor[2] = 20;      
        myVtor[3] = 30;      
        myVtor[4] = 40;          

        int myInt =  myVtor.front();

        cout << myInt << '\n';


        return 0;
    }

const_reference front() const;
This is similar to “reference front();”, but the value returned cannot be changed. Read and try the following code:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);

        myVtor[0] = 0;    
        myVtor[1] = 10;      
        myVtor[2] = 20;      
        myVtor[3] = 30;      
        myVtor[4] = 40;          

        const int myInt =  myVtor.front();

        cout << myInt << '\n';


        return 0;
    }

Note the use and position of the reserved word, const, for the receiving object.

reference back();
This is a method that returns the reference to the last element of the vector list. The reference is equivalent to the value of the last element. Read and try the following program:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);

        myVtor[0] = 0;    
        myVtor[1] = 10;      
        myVtor[2] = 20;      
        myVtor[3] = 30;      
        myVtor[4] = 40;          

        int myInt =  myVtor.back();

        cout << myInt << '\n';


        return 0;
    }

const_reference back() const;
This is similar to “reference back();”, but the value returned cannot be changed. Read and try the following program:

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
        vector<int> myVtor(5, 2);

        myVtor[0] = 0;    
        myVtor[1] = 10;      
        myVtor[2] = 20;      
        myVtor[3] = 30;      
        myVtor[4] = 40;          

        const int myInt =  myVtor.back();

        cout << myInt << '\n';


        return 0;
    }

Note the use and position of the reserved word, const, for the receiving object.

We have seen all the eight vector element access commands. We can take a break and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message