2

New to C++ here. I have a pointer variable type vector, which I initialized to be n by m, where n and m are int's given as input. Here is how I initialize it.

std::vector<std::vector<int>>* memo; // This is as a member.

void test(int m, int n) {
    memo = new std::vector<std::vector<int>>(m, *new std::vector<int>(n, 0)); // This is inside a method.
}

Later, I try to assign a certain element.

int ans = 5; // In my actual code, it's something else, but I'm just simplifying it.
memo[i][j] = ans; // This gives an error.

I thought I just needed to deference it, because right now it is a pointer type. So I changed it to this:

*memo[i][j] = ans;

However now I got a new error:

C++ no operator matches these operands            operand types are: * std::vector<int, std::allocator<int>>

Why isn't this working, and how can I make it work?

  • Chances are you don't want a `std::vector*` type, but a `std::vector` type. C++ is sometimes hard, but not impossible to learn right. Ditch the pointers and you are good to go. – Ron Mar 19 '20 at 19:12
  • There's almost never a reason to have a pointer to a container. What's worse, because of `*new std::vector(n, 0)` you will have a *memory leak*. Are you coming from a Java or C# background where objects must be created with `new`? In C++ you don't have to do that. I suggest you invest in [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Mar 19 '20 at 19:12
  • As for the problem with `*memo[i][j]`, due to [*operator precedence*](https://en.cppreference.com/w/cpp/language/operator_precedence) it's equal to `*(memo[i][j])`, which isn't correct to begin with (with `memo` being a pointer). You need to use `(*memo)[i][j]`. But as I already said, don't have a pointer to begin with, and the problem goes away. – Some programmer dude Mar 19 '20 at 19:14

1 Answers1

1

For starters this declaration

std::vector<std::vector<int>>* memo;

does not make great sense. It would be better to declare the data member like

std::vector<std::vector<int>> memo;

This allocation

memo = new std::vector<std::vector<int>>(m, *new std::vector<int>(n, 0));

Is incorrect. The elements of the vector are not pointers. You should write

memo = new std::vector<std::vector<int>>(m, std::vector<int>(n, 0)); 

or

memo = new std::vector<std::vector<int>>(m, std::vector<int>(n ));

It seems it is the reason of other your problems.

To set a value to such a vector you should write for example

( *memo )[i][j] = ans;

If to declare the data member as not a pointer like for example

std::vector<std::vector<int>> memo;

then in a function you could assign it like

memo.assign( m, std::vector<int>( n ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks! I never knew you could use the `new` keyword on non-pointer types before. –  Mar 19 '20 at 19:15
  • @NishantChatterjeeKMS You are mistaken. The allocation showed for your declaration of a pointer to vector.:) – Vlad from Moscow Mar 19 '20 at 19:16
  • So this declaration is fine for a non-pointer type vector? `memo = std::vector>(m, std::vector(n));` –  Mar 19 '20 at 19:18
  • @NishantChatterjeeKMS You could set it such a way in mem-initializer lisy of a constructor. Otherwise you can use for example the method assign. See my updated answer. – Vlad from Moscow Mar 19 '20 at 19:20