Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Your choice should reflect your needs. All the elements of the vectors are continous in memory and lists has pointers to next / previous elements so they each have their advantage / disavantages :

Lists :

  • Each elements takes 2 integers to point previous and next elements, so most commonly, that's 8 bytes more for each elements in your list
  • Insert is linear in time : O(n)
  • Remove is a constant operation : O(1)
  • Access the x element is linear in time : O(n)

Vectors :

  • Needs less memory (no pointers to other elements, it's a simple math algorithm)
  • Remove is linear in time : O(n)
  • Access the x element is constant : O(1) (That's because the elements are continious in memory so it's a simple math operation vectorPtr + ( x * bytesOfTheType ) )
  • Insert can be in linear time, but most commonly, it's a constant operation : O(1) (That's because the vector in an array but always reserve 2 times it's capacity when the array is full so array copy is not frequent )

So list is better when you program needs to add and remove elements frequently, but never access (or rarely access) a particular element without the need of the others before. The vector should be used for better access time, but lacks effeciency when you need to remove or add elements.

Check this post on stackoverflow, it present a really nice graph with basic questions on your needs that drives you to a specific container depending on your answers :

http://stackoverflow.com/questions/366432/extending-stdlisthttps://stackoverflow.com/questions/366432/extending-stdlist

Your choice should reflect your needs. All the elements of the vectors are continous in memory and lists has pointers to next / previous elements so they each have their advantage / disavantages :

Lists :

  • Each elements takes 2 integers to point previous and next elements, so most commonly, that's 8 bytes more for each elements in your list
  • Insert is linear in time : O(n)
  • Remove is a constant operation : O(1)
  • Access the x element is linear in time : O(n)

Vectors :

  • Needs less memory (no pointers to other elements, it's a simple math algorithm)
  • Remove is linear in time : O(n)
  • Access the x element is constant : O(1) (That's because the elements are continious in memory so it's a simple math operation vectorPtr + ( x * bytesOfTheType ) )
  • Insert can be in linear time, but most commonly, it's a constant operation : O(1) (That's because the vector in an array but always reserve 2 times it's capacity when the array is full so array copy is not frequent )

So list is better when you program needs to add and remove elements frequently, but never access (or rarely access) a particular element without the need of the others before. The vector should be used for better access time, but lacks effeciency when you need to remove or add elements.

Check this post on stackoverflow, it present a really nice graph with basic questions on your needs that drives you to a specific container depending on your answers :

http://stackoverflow.com/questions/366432/extending-stdlist

Your choice should reflect your needs. All the elements of the vectors are continous in memory and lists has pointers to next / previous elements so they each have their advantage / disavantages :

Lists :

  • Each elements takes 2 integers to point previous and next elements, so most commonly, that's 8 bytes more for each elements in your list
  • Insert is linear in time : O(n)
  • Remove is a constant operation : O(1)
  • Access the x element is linear in time : O(n)

Vectors :

  • Needs less memory (no pointers to other elements, it's a simple math algorithm)
  • Remove is linear in time : O(n)
  • Access the x element is constant : O(1) (That's because the elements are continious in memory so it's a simple math operation vectorPtr + ( x * bytesOfTheType ) )
  • Insert can be in linear time, but most commonly, it's a constant operation : O(1) (That's because the vector in an array but always reserve 2 times it's capacity when the array is full so array copy is not frequent )

So list is better when you program needs to add and remove elements frequently, but never access (or rarely access) a particular element without the need of the others before. The vector should be used for better access time, but lacks effeciency when you need to remove or add elements.

Check this post on stackoverflow, it present a really nice graph with basic questions on your needs that drives you to a specific container depending on your answers :

https://stackoverflow.com/questions/366432/extending-stdlist

Source Link

Your choice should reflect your needs. All the elements of the vectors are continous in memory and lists has pointers to next / previous elements so they each have their advantage / disavantages :

Lists :

  • Each elements takes 2 integers to point previous and next elements, so most commonly, that's 8 bytes more for each elements in your list
  • Insert is linear in time : O(n)
  • Remove is a constant operation : O(1)
  • Access the x element is linear in time : O(n)

Vectors :

  • Needs less memory (no pointers to other elements, it's a simple math algorithm)
  • Remove is linear in time : O(n)
  • Access the x element is constant : O(1) (That's because the elements are continious in memory so it's a simple math operation vectorPtr + ( x * bytesOfTheType ) )
  • Insert can be in linear time, but most commonly, it's a constant operation : O(1) (That's because the vector in an array but always reserve 2 times it's capacity when the array is full so array copy is not frequent )

So list is better when you program needs to add and remove elements frequently, but never access (or rarely access) a particular element without the need of the others before. The vector should be used for better access time, but lacks effeciency when you need to remove or add elements.

Check this post on stackoverflow, it present a really nice graph with basic questions on your needs that drives you to a specific container depending on your answers :

http://stackoverflow.com/questions/366432/extending-stdlist