0

I have defined:

const  vector<vector<int>> *ElementLines::Quad4 = new vector<vector<int>>
{
    { 0, 1 },
    { 1, 2 },
    { 2, 3 },
    { 3, 0 }
};

Later on, I want to iterate over that collection, to which an object is pointing:

for (int j = 0; j < e->LinesIndices->size(); j++)
        {
            int n1Index = e->LinesIndices[j][0]; //I expect 0 (for j = 0)
            int n2Index = e->LinesIndices[j][1]; //I expect 1 (for j= 0)
        }

The code above won't compile:

no suitable conversion function from "const std::vector<int, std::allocator<int>>" to "int" exists  

But if I add LinesIndices[j][0][0] it indeed delivers an int. I don't quite understand what is happening here. To access a vector I just use one pair of square brackets [i], is it different for this nested vector of vectors? (I would expect to be able accessing the contents by using two pairs of square brackets).

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • 2
    You have a pointer to vector of vector of int. You are indexing into the pointer, not the top-level vector. – hlt Apr 11 '17 at 08:58
  • @hlt I'm was not able to figure out my issue with your hint. Obviously I lack of basic knowledge. Could you show what would be the correct syntax to access the contents of the internal vectors? – Sturm Apr 11 '17 at 09:05
  • Writing up a proper answer, give me a minute ;) – hlt Apr 11 '17 at 09:05
  • I guess that I should access the pointed value of LinesIndices item before trying to access it's contents: 'int n1Index = (*e->LinesIndices)[j][0];' Is this what I should be doing? – Sturm Apr 11 '17 at 09:09
  • @Sturm What is e and LinesIndices and how are they related to Quad4? – Vlad from Moscow Apr 11 '17 at 09:09
  • e it is an instance of a type that has a member called LinesIndices which is indeed a pointer to vector> – Sturm Apr 11 '17 at 09:10
  • BTW, you probably want `vector>` without pointer. – Jarod42 Apr 11 '17 at 09:29

1 Answers1

5

Your code is not compiling because your e->LinesIndices is a vector<vector<int>>* (i.e. a pointer).

In C++, as in C, you can use array notation on pointers—a[index] is equivalent to *(a + index). If your pointer pointed to the first element of an array, that is exactly how you would use that array. Unfortunately for you, you only have a single vector allocated via new. Accessing that pointer via e->LinesIndices[j] is a Very Bad Thing if j is not 0 (because you access a vector where there is no actual vector).

There is two ways to fix this. If you really want to keep your vector on the heap, allocated via new (I hope you delete it at some point!), you could dereference the pointer before accessing it:

for (int j = 0; j < e->LinesIndices->size(); j++)
{
    int n1Index = (*e->LinesIndices)[j][0];
    int n2Index = e->LinesIndices[0][j][1]; // This would work too, but I wouldn't recommend it
}

However, the data in your vector is already on the heap. Allocating a std::vector via new is something that is—in my personal experience—very rarely necessary, and if it is not necessary for you to have a pointer here (which will heavily depend on the context you use it in), I would suggest directly creating the vector (with no pointer). If you choose this method, you will need to use e->LinesIndices.size() instead of e->LinesIndices->size().

Community
  • 1
  • 1
hlt
  • 6,219
  • 3
  • 23
  • 43