How to Access Member Functions From Pointer to a Vector in C++
-
Use
->
Notation to Access Member Functions From Pointer to a Vector -
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::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;
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 FacebookRelated Article - C++ Function
- C++ Redefinition of Formal Parameter
- The ShellExecute() Function in C++
- The const Keyword in Function Declaration of Classes in C++
- How to Handle Arguments Using getopt in C++
- Time(NULL) Function in C++
- Cotangent Function in C++
Related Article - C++ Pointer
- Dangling Pointers in C++
- Declaration and Uses of unique_ptr in C++
- Function Pointer to Member Function in C++
- How to Swap Two Numbers Using Pointers in C++
- Void Pointer in C++
- Functionality and Difference Between *& and **& in C++