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

Jinku Hu Feb 02, 2024
  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
How to Access Member Functions From Pointer to a Vector in C++

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::cin;
using std::cout;
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::cin;
using std::cout;
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::cin;
using std::cout;
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;
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Function

Related Article - C++ Pointer

Related Article - C++ Vector