简体   繁体   中英

vector<vector<largeObject>> vs. vector<vector<largeObject>*> in c++

Obviously it will vary depending on the compiler you use, but I'm curious as to the performance issues when doing vector<vector<largeObject>> vs. vector<vector<largeObject>*> , especially in c++. In specific:

let's say that you have the outer vector full, and you want to start inserting elements into first inner vector. How will that be stored in memory if the outer vector is just storing pointers, as apposed to storing the whole inner vector. Will the whole outer vector have to be moved to gain more space, or will the inner vector be moved (assuming that space wasn't pre-allocated), causing problems with the outer vector?

Thank you

Vector is internally a pointer so pointer of vector is sort of overkill.

Vector of pointers or smart pointers is usually used when polymorphic contents are needed.

In C++03 it may be expensive to insert more vectors or (erase existing ones) of your master vector but C++0x resolves even that issue with its move semantics.

It is better to switch to more suitable containers later after profiling with real data instead of trying to make it initially extremely dynamic.

My first question would be "why are you using nested vectors?" If you don't need the dimensions of the "2D array" to be jagged, you can use a single vector and canonical 2D indexing into a 1D array (x + width * y).

That said, since vector copies T instances around when resizing, et cetera, a vector of pointers to large objects will likely be less expensive because the copies will be smaller (copying a pointer versus a "large object"). The down side is that you'll have to manage the allocations of the large objects yourself, but something like boost's shared_ptr can help (or the standard version if you have 0x support -- not auto_ptr, though). "Large object" here could be anything, including nested vectors or pointers-to-vector like in your original example if need be.

EDIT: You can also use reserve() to pre-allocate space in the vector, preventing a lot of copying if you can make guarantees about the amount of stuff you're going to shove into the vector.

I think there will be no big difference in performance in your case it is just the matter of when do you want to spend time to create objects.

Some times it is better to use vector<vector<BigObject*>>

The answer with performance questions is always: performance test or profile it.

Also consider the alternatives:

vector< vector< LargeObject* > >, 

or

vector< vector< shared_ptr<LargeObject> > >

What works best will depend on the operations you perform:

  • Lots of constructions/ destructions
  • Copies - Can they be optimised to use vector::swap()?
  • Do you erase elements from the middle of the vector?

Do you want to handle the manual memory management? If the answer is no, the stick with vector > until there is a demonstrated performance issue.

To protect yourself against having to change too much code for performance reasons later, you could/should encapsulate the 2D array in a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM