-5

How do I create and access a vector of pointers to vectors. What I mean by this is:

std::vector<size_t>, vec1, vec2, vec3;

int main() {
    std::vector<std::vector<size_t>*> vecP {
        &vec1,
        &vec2,
        &vec3
    };
}

initializing vecP like this seems to work, or at least in Visual Studio, it doesn't give any red underlines indicating a syntax error. However if I try to access it like this:

    //continuing int main()
    vecP[1].resize(6);
    //or
    vecP[1][3] = 7;

it doesn't work.

Something about expression must have class type, if I replace the vecP's in the above block with vecP it will say no operator "" matches these operands.

I'm pretty new to the idea of pointers in c++ and I'm sure the solution is simple, but regardless all help is appreciated.

Matthew
  • 185
  • 1
  • 12
  • 2
    Learn when to use the `->` syntax. – PaulMcKenzie Apr 22 '18 at 17:32
  • 4
    ***Why*** do you want to use pointers to vectors? What problem is that supposed to solve? – Some programmer dude Apr 22 '18 at 17:33
  • @PaulMcKenzie I've seen them before, and I looked into it but I can't seem to find anything online that explains in a way I can understand, I definitely learn better by studying other peoples code and seeing what does what. – Matthew Apr 22 '18 at 17:33
  • 5
    I recommend you [get a couple of good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) instead. – Some programmer dude Apr 22 '18 at 17:34
  • 1
    @Someprogrammerdude that's irrelevant to the question but the answer is simply because I need to change the values of vec1 vec2 etc, but I'm putting those in a vector because I'm using a for loop to get through each of them. Also I'll take your book suggestion, if I can find any for free or at a local library anyway. I'm surprised they still make them instead of just publishing them online. – Matthew Apr 22 '18 at 17:34
  • @Magicrafter13Gaming -- The issue is not really `vector`. How do you access members of any class when given a pointer to an instance of the class? You could learn yourself by simplifying what you're not clear about. – PaulMcKenzie Apr 22 '18 at 17:37
  • @Magicrafter13 Gaming `std::vector` needs a init size, use std::map instead... – MindLerp Apr 22 '18 at 17:38
  • It might be irrelevant for the question itself, but it *is* something which could be seen as an implementation of a bad design. A beginner in C++ should never have to deal with *pointers* to vectors. Even seasoned C++ programmers don't use it, the number of cases where it's needed is abysmal. – Some programmer dude Apr 22 '18 at 17:38
  • if I do vecP[1]->resize(6); it appears to work. still can't do vecP[1][3] = 7; – Matthew Apr 22 '18 at 17:47
  • Working with pointers is one of the most **basic** aspects of C/C++ programming; there are many online tutorials which offer very clear and concise explanations for beginners; if you can't understand them then I doubt anyone here would be able to explain better. – meowgoesthedog Apr 22 '18 at 17:48
  • And btw, either `*(vecP[1])[3]` or `vecP[1]->operator[] (3)`. – meowgoesthedog Apr 22 '18 at 17:48
  • You have pointers to individual vectors but you are accessing them as if they were pointers to *arrays* of vectors. Can you not just have a vector of vectors? What are the pointers for? – Galik Apr 22 '18 at 17:58
  • @Galik because I need the original vectors to have their values assigned, not just a copy. – Matthew Apr 22 '18 at 18:09
  • `still can't do vecP[1][3] = 7;` @Magicrafter13Gaming `vecP[1]->resize(6); vecP[1]->operator[](3) = 7;` – Killzone Kid Apr 22 '18 at 18:40
  • @PaulMcKenzie after messing around with -> for a while I did get my thing to work. Thank you. – Matthew Apr 22 '18 at 18:44

1 Answers1

1

Given your declarations, you need

vecP[1]->resize(6);
// or
vecP[1]->at(3) = 7;

The problem being that since your (top level) vector elements are pointers, you need to dereference them to access their contents. The easiest way of dereferencing is to use -> instead of ., as I did above, but you can also use (unary) *.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226