Initialize vector of vectors after declaration.

Started by
17 comments, last by Servant of the Lord 9 years, 1 month ago

The answers you gave actually works, but is there any actual difference between one aproch and another?(vector of vectors vs. vector+arithmetic index).

Yep, there are differences.

  • An array of arrays is not the same as a "2D array". They are different concepts.
  • With an array of arrays, every row can be a different size, which means keeping them in-sync can be a nuisance, depending on what you're doing.
  • Using math means you can treat the array as 1D when it benefits you, and 2D when it benefits you. Best of both worlds.
    One example of this is iterating over every element - you can iterate over it as 1D when it doesn't matter what order you are accessing them in.
  • A vector of vectors keeps every vector in a different location of memory, so it can't be cached as effectively. If the code is at very low level, this might be important. If it's high-level, it doesn't matter much.

Learning new ways to do something helps to exercise your mind. wink.png

Advertisement

Addressing the adjacent issue, std::array allows for static-length containers.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

There's also a way to use 1d array as a logical 2d array while keeping the [][] indexing syntax. Basically you have your Wrapper class that has an operator[]. This operator[] returns a light weight "view" object that defines the operator[] as well. The [] operator of this view object then returns the item you want.


Grid g(10, 5);
View v = g[2];
Item i = v[3];
 
// or simply
Grid g(10, 5);
Item i = Grid[2][3];

Basically indexing the grid by x gives you a closure, and calling the closure with y effectively gives you the item at grid[x][y].

edit:

http://www.gamedev.net/topic/665945-easiest-way-to-pass-a-2d-array/#entry5211935

looks like someone else already gave an example of this, in a different thread on a similar topic

std::vector<std::vector<int> > myArray;

You must realize that this is quite improper.

You have used template of a variable memory type as another vector template type.

If you want to reference unknown amount of static arrays, pooled in memory, you were given suggestions.

For example as :

class CConcreteArray

{

public:

float m_Arr[115];

}

std::vector<CConcreteArray > myArray;

Why would you use that instead of the much more convenient std::array?

std::vector<std::vector<int> > myArray;

You must realize that this is quite improper.

You have used template of a variable memory type as another vector template type.

I don't know if I'd call it "improper". OP wants member containers of static length, so a std::array is better than a std::vector. A vector of arrays will be contiguous in memory, which is nice. However, sometimes you actually do want a variable-length container of variable-length containers, so a vector of vectors would make sense. The templates don't really have much to do with it. It's perfectly legal to nest template types.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

std::vector<std::vector<int> > myArray;

You must realize that this is quite improper.

You have used template of a variable memory type as another vector template type.

I don't know if I'd call it "improper". OP wants member containers of static length, so a std::array is better than a std::vector. A vector of arrays will be contiguous in memory, which is nice. However, sometimes you actually do want a variable-length container of variable-length containers, so a vector of vectors would make sense. The templates don't really have much to do with it. It's perfectly legal to nest template types.

I was not proclaiming something over std::array, and do you suspect my solution is not interleaved in memory? Variable array of variable arrays, nice thing (what std::array is not either), but what OP did was array of vector types, what is legal to do but does not result in interleaved arrays in memory.

Yeah, you were right, sorry for disturbing you. I was wrong in every little thing I have said. ihumw

What is going on then? I am a foolish stupid right?

The person who rated you down wasn't Khathar, or myself, or anyone else in the thread.

Why specifically, someone rated you down, I'm not sure. One possible reason is that you've repeated things that other people in the thread have already mentioned.

But I think people were confused about what you were trying to communicate. It almost sounds like you were saying that you can't use templated types within other templated types, which isn't correct. But I think you were saying that using a dynamically sized array within other dynamically sized arrays isn't the best choice in this situation.

In a programming environment, being precise or technically correct is really important. We're very finicky. The language barrier can sometimes get in the way of that.

This topic is closed to new replies.

Advertisement