How to pick a pointer from a class c++ vector

Asked

Viewed 186 times

0

How do I pick a pointer from a vector of class c++? For example I have a vector a; how do I receive a pointer to only one vector element?

1 answer

2


From c++11 you can use the function date. It returns the address of the array used to store the vector elements. Note that if the vector has no elements, data() may or may not return nullptr

#include <iostream>
#include <vector>

int main() {
  std::vector<int> cheio{1, 2, 3};
  std::vector<int> vazio;
  cheio.clear();
  std::cout << std::boolalpha << "vazio.data() == nullptr? "
            << (vazio.data() == nullptr) << "\n"
            << "cheio.data() == nullptr? " 
            << (cheio.data() == nullptr) << "\n";
}
vazio.data() == nullptr? true
cheio.data() == nullptr? false
  • 1

    This answer is falling under "Low quality publications" due to its size. It could make your answer clearer?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.