Addressing a vector inside a vector of vector pointers

To implement an open hash table in C ++, I thought I would define a vector containing pointers to vectors containing data. For simplicity, let me say that I want a hash table that can store ints. I suggested that I need this vector< vector<int>* >.

The resulting data structure might look something like this:

[index 0] 8, 6, 2

[index 1] (empty)

[index 2] 9, 12, 15, 28, 1

I could create a static array of pointers vector<int>, but I wanted to be able to add more indexes as I go.

To record the elements, I wanted to do something like this:

for (unsigned int i = 0; i < myHashtable.size(); i++) {
  cout << "[index " << i << "]";
  for (unsigned int j = 0; j < myHashtable[i]->size(); j++) {
    cout << " " << *(myHashtable[i])[j];
  }
  cout << "\n";
}

This code does not compile. What is the right way to turn to *(myHashtable[i])[j]?

+3
4
(*myHashtable[i])[j]

subscript ([]) , (*) (. http://www.cppreference.com/wiki/operator_precedence). , .

, :

for (vector<int>::iterator it = myHashtable[i]->begin();
     it != myHashtable[i]->end(); ++it)
{
    cout << " " << *it;
}
+7

(*(myHashTable[i]))[j].

+3

Just (* myHashtable [i]) [j]

+3
source

(*myHashtable[i])[j], because the operator []has higher accuracy than the operator *.

+2
source

Source: https://habr.com/ru/post/1773491/


All Articles