473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vector or list ---> confused

180 100+
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
10 1576
JosAH
11,448 Expert 8TB
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
vermarajeev
180 100+
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
JosAH
11,448 Expert 8TB
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
vermarajeev
180 100+
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
JosAH
11,448 Expert 8TB
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
9,208 Expert Mod 8TB
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
153 100+
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
JosAH
11,448 Expert 8TB
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
9,208 Expert Mod 8TB
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

2
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
1
by: Steffo | last post by:
Why can't I use stl list in my dll. I'ts no problem with vector, but when trying list I get the followning error msg: error C2061: syntax error : identifier 'list' Hu!! S.
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
5
by: Numeromancer | last post by:
From the C++-FAQ Lite: http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3 ---------------------------- 34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes. ...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.