Access Member Functions From Pointer to a Vector in C++

  1. Use -> Notation to Access Member Functions From Pointer to a Vector
  2. Use (*)vector.member Notation to Access Member Functions From Pointer to a Vector

This article will explain several methods of how to access member functions from a pointer to a vector in C++.

Use -> Notation to Access Member Functions From Pointer to a Vector

vector member functions can be called from the pointer to the vector with the -> operator. In this case, we are passing the pointer to the vector to a different function. As member functions, any data member of the struct/class needs to be accessed using -> notation. Note that, when passing the pointer to the object to the different function, the address of the operator (&) should be used before the variable.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < vec->size(); ++i) {
        cout << vec->at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};

    printVectorElements(&str_vec);

    return EXIT_SUCCESS;
}

Output:

bit; nibble; byte; char; int; long; long long; float; double; long double;

Use (*)vector.member Notation to Access Member Functions From Pointer to a Vector

Accessing the value pointed to by the pointer is done using a dereference operation, which can substitute the -> operator functionally and improve readability. (*vec).at(i) expression can essentially do the same operation of member access. Note that we are using the function template that can print a vector with generic type elements.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < (*vec).size(); ++i) {
        cout << (*vec).at(i) << "; ";
    }
    cout << endl;
}

int main() {
    vector<string> str_vec = {"bit", "nibble",
                              "byte", "char",
                              "int", "long",
                              "long long", "float",
                              "double", "long double"};

    printVectorElements(&str_vec);

    return EXIT_SUCCESS;
}

Output:

bit; nibble; byte; char; int; long; long long; float; double; long double;

Alternatively, suppose the pointer to the vector is allocated using the new function call, and the variable is not passed to the different function. In that case, we can choose any of the above notations. In the following example, we demonstrate the vector element iteration and output operation using the -> operator.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
void printVectorElements(vector<T> *vec)
{
    for (auto i = 0; i < vec->size(); ++i) {
        cout << vec->at(i) << "; ";
    }
    cout << endl;
}

int main() {
    auto *i_vec = new vector<int>(10);
    printVectorElements(i_vec);
    for (int i = 0; i < 10; ++i) {
        i_vec->at(i) = i + 1;
        cout << i_vec->at(i) << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output (if the input is +):

0; 0; 0; 0; 0; 0; 0; 0; 0; 0;
1; 2; 3; 4; 5; 6; 7; 8; 9; 10;

Related Article - C++ Function

  • Solve Control Reaches End of Non-Void Function Error in C++
  • Use Callback Functions in C++
  • Related Article - C++ Pointer

  • Check if Pointer Is NULL in C++
  • Create Vector of Pointers in C++
  • Related Article - C++ Vector

  • Initialize Vector of Structs in C++