0

I remember c++ primer said that 2d vector is very inefficient and should be avoided. but 2d array seems to be rather inconvenient in terms of both creating and deleting.

is there any other way to do it? or 2d vector still competable against 2d array?

2
  • 2
    If you are specifically talking about C++ then you should tag your question with the C++ tag. Also I would suggest being more specific about how you intend to use them, and what your definition of "inefficient" is, it seems a very broad question as it stands Commented Aug 4, 2016 at 10:39
  • @musefan yes, I am talking abt c++, sorry for the missing taps, I forgot about it. Commented Aug 4, 2016 at 11:14

1 Answer 1

2

I doubt that it matters. There are other factors that will matter more.

I would question the starting premise:

2d vector is inefficient

Sometimes we trade off pure speed for better abstraction. I'll bet the std::string class can be considered inefficient by some measures when compared to raw byte or character array, but I'd still use it.

You'll have a better case if you stop worrying about broad statements and focus on your use case.

The most common application of 2D arrays I know of is for vectors, matricies, and linear algebra. There are other factors for that problem that will be far more important than the choice of underlying data structure.

Since C++ is an object-oriented language, you can solve this easily by starting with an interface and creating implementations that use vector and array. Test them against a meaningful data set and measure.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the reply. actually I am testing some matrix algorithm. For my personal tests, the speed does not rly matter that much. But I am just wondering if someday I do need to do same the thing in a big project which would be better. for example let's say, a simple matrix multiplication.
You should worry more about sparsity, parallelization, and getting the abstraction right; less about data choosing between vector and array. Have you done a buy versus build analysis? Why would you write your own linear algebra library?
Hi, I am reading a book about algorithm and I want to test some of the pseudo codes. I haven't met any similar problems in my job, but as i said just wondering if it goes significantly expensive if I do implement it with 2d array if the variable size goes up.
Do what I suggested: Create a pure virtual interface and implementations for each one underneath. Measure them against a number of meaningful data sets and use cases and compare timings.
Variable size will only become a concern if you have very, very large matricies to deal with. When that happens you'll have bigger things to worry about. This is a premature optimization. You should not allow your focus on this problem to prevent you from moving forward. I always advise using a better abstraction. Go with vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.