By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

vector or list ---> confused

100+
P: 180
Hi guys,
I know what a vector and a list is?

I have this scenario and I want to know which will be best to use.

I have a class called "molecule". The maximum number of atoms a molecule can hold is 72. The number of atoms vary depending on the type of molecule, it can be 4 or 10 or 1 or 50 etc etc...upto maxatoms.

Hence, since the maximum number of atoms is fixed i.e 72, and for current molecule if the number of atoms is only 4, there are 68 vacant space empty.
A big "waste of space".
Also I want to randomly access the atoms, to test certain conditions.
Currently I use vector<molecule> but I think using vector can help me randomly access the atoms but eats up more memory. Dont you think?

So what do you think can be best "vector/list" for above discussed scenario.

Thanks in advance
Aug 19 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 10K+
P: 11,448
Also I want to randomly access the atoms, to test certain conditions.
The magic phrase here is "random access"; so use a vector.

kind regards,

Jos
Aug 19 '07 #2

100+
P: 180
The magic phrase here is "random access"; so use a vector.

kind regards,

Jos
Dont you think using a vector in the above scenario is a big waste of memory though is helps me to "randomly access" the elements?

Thanks
Aug 19 '07 #3

Expert 10K+
P: 11,448
Dont you think using a vector in the above scenario is a big waste of memory though is helps me to "randomly access" the elements?

Thanks
Nope, a vector doesn't contain a fixed number of elements, e.g. if you have a
vector containing 68 elements the vector will take up more memory than a
vector containing just 2 elements; vectors aren't fixed size arrays.

kind regards,

Jos
Aug 19 '07 #4

100+
P: 180
Expand|Select|Wrap|Line Numbers
  1.    ______________________
  2.   |___mol1____|__mol2____|         
  3.           |            |
  4.           |            |____________________
  5.           |            |_1_|_2_|_3_|_.....|_72__|         
  6.       ____|________________
  7.       |_1_|_2_|_3_|_.....|_72__|
  8.  
  9.  
Assume there are two molecules mol1, mol2.
mol1 has allocated space for maxatoms 72, and mol2 has allocated space for maxatoms 72.
Just think mol1 is actually a molecule having only 4 atoms and mol2 is actually a molecule having only 52 atoms. Dont worry about how or why just assume.

Now, dont you think 68 spaces are wasted in case of mol1 and 20 spaces in case of mol2 ?
This was my question. Hence can you now tell me using a vector is better or a list?
Since list is a doubly linked list, I think using a list should save up memory. But on the other hand I cannot use subscripts to access data "randomly".
Aug 19 '07 #5

Expert 10K+
P: 11,448
Expand|Select|Wrap|Line Numbers
  1.    ______________________
  2.   |___mol1____|__mol2____|         
  3.           |            |
  4.           |            |____________________
  5.           |            |_1_|_2_|_3_|_.....|_72__|         
  6.       ____|________________
  7.       |_1_|_2_|_3_|_.....|_72__|
  8.  
  9.  
Assume there are two molecules mol1, mol2.
mol1 has allocated space for maxatoms 72, and mol2 has allocated space for maxatoms 72.
Just think mol1 is actually a molecule having only 4 atoms and mol2 is actually a molecule having only 52 atoms. Dont worry about how or why just assume.

Now, dont you think 68 spaces are wasted in case of mol1 and 20 spaces in case of mol2 ?
This was my question. Hence can you now tell me using a vector is better or a list?
Since list is a doubly linked list, I think using a list should save up memory. But on the other hand I cannot use subscripts to access data "randomly".
When you use vectors for that, there are no 68 or 20 wasted spaces, i.e. a
vector grows on demand. A vector is not like a statically sized array.

kind regards,

Jos
Aug 19 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
When you use vectors for that, there are no 68 or 20 wasted spaces, i.e. a
vector grows on demand. A vector is not like a statically sized array.
Actually, I think it is a statically sized array. I believe vector must be implemented as an array. It;s just that when the vector needs to expand, a larger array is allocated and a copy occurs. That is, this must work for a vector:

Expand|Select|Wrap|Line Numbers
  1. vector<int> v;
  2. v.push_back(10);
  3. int * ptr = &v[0];
  4. *ptr = 20;   //changes the 10  to a 20
  5.  
Aug 19 '07 #7

kreagan
100+
P: 153
Why are you concerned about memory? (Just wondering) Is your program running sluggish or eating too much memory?

According to the articles below, vectors reserve a chunk of memory. Where as reserving the memory is a "waste of space" and theoretically the List would conserve more memory. However, memory management isn't perfect and deallocating and allocating memory can create fragmented memory. This can result in wasted memory also. I would suggest looking at the data structure with the best functionality until memory becomes an issue.

Anyways, here's some links to read about Vectors and Linked Lists:
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/

"Compared to the other base standard sequence containers (deques and lists), vectors are generally the most efficient in time for accessing elements and to add or remove elements from the end of the sequence. For operations that involve inserting or removing elements at positions other than the end, they perform worse than deques and lists, and have less consistent iterators and references than lists."

"Compared to other base standard sequence containers (vectors and deques), lists perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position;"
Aug 19 '07 #8

Expert 10K+
P: 11,448
Actually, I think it is a statically sized array. I believe vector must be implemented as an array. It;s just that when the vector needs to expand, a larger array is allocated and a copy occurs. That is, this must work for a vector:

Expand|Select|Wrap|Line Numbers
  1. vector<int> v;
  2. v.push_back(10);
  3. int * ptr = &v[0];
  4. *ptr = 20;   //changes the 10  to a 20
  5.  
Nope, just suppose you're doing another push_back afterwards: this is what I
ripped from the STL documentation:

Reallocations invalidate all previously obtained iterators, references and pointers.

Those arrays used by vectors are resized whenever necessary. They don't have
a fixed size (that's a better phrase for it than 'statically sized' which I used previously).

kind regards,

Jos
Aug 19 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
Reallocations invalidate all previously obtained iterators, references and pointers.
True. But until then, the vector is a fixed array. After the push_back() there may (or may not) have been allocated a new array. While the pointers and interators may have been invalidated, the vector still contains an array.
Aug 19 '07 #10

Expert 10K+
P: 11,448
True. But until then, the vector is a fixed array. After the push_back() there may (or may not) have been allocated a new array. While the pointers and interators may have been invalidated, the vector still contains an array.
Yep, I never argued against that ;-) All I say is that that array may be reallocated
and the size will be different after that reallocation. Any decent implementation
'minimizes the overhead' of unused array elements, so I advised the OP to use
a vector for his/her purposes.

kind regards,

Jos
Aug 19 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.