0

As you know, there are many options for grouping objects in c++ but each of them has cons and pros, but for now, I am only talking about Lists and Vectors.

So, Which one (List and Vector) to choose when Getting an Element at index is under pressure (called more (A lot of times))

The decision must depend on the performance



| For example, Lists must have iterators:

std::list<std::string>::iterator it = listOfStrs.begin();

std::advance(it, 2);

| And Vectors can be directly used with an operator:

std::vector<int> vecOfNums{ 1, 4, 5, 22, 33, 2, 11, 89, 49 };

int & element = vecOfNums[3];

In a nutshell, the question is Which method of getting an element from an index is faster?

A relative question for phyton: Why is [] faster than list()?

Explanation for my case so the question doesn't become an XY question:

Well, as it is really obvious I am trying to iterate through a group of elements/objects, these objects are Classes that are defined in a header file... I am using These classes to indicate Commands because I am making my own programming language and to do this I need a compiler as it is reallllllyy obvious I need to iterate through the commands and I just need to know which method is faster Lists or Vectors

Edit:

I want to iterate through the whole group so not a random element

Ramsey
  • 104
  • 8
  • 1
    Anytime you have to iterate a linked list to find data, which you pretty much always have to do if you access linked list by index, you're taking a performance hit, usually a significant one, over random access. – user4581301 May 26 '22 at 22:03
  • 1
    Your question initially seems different from what the last paragraph asks for. There is a difference between the indexing a random element of the container and iterating over the whole container in order. Which of the two are you interested in? And for iterating in order you wouldn't use `std::advance`. – user17732522 May 26 '22 at 22:04
  • Handy reading: [Are lists evil?](https://isocpp.org/blog/2014/06/stroustrup-lists) – user4581301 May 26 '22 at 22:06
  • Note that C++ lists and Python lists have nothing in common except the name. – Mark Ransom May 26 '22 at 22:06
  • @MarkRansom I linked that for the users that can accidently come here while searching for phyton should I better remove it? – Ramsey May 26 '22 at 22:07
  • The linked duplicate answers the question for iterating in-order (rather than random access). – user17732522 May 26 '22 at 22:07
  • @user17732522 Thanks I literally didn't see the duplicate post after writing my question I event checked twice sorry for that – Ramsey May 26 '22 at 22:10
  • Isn't this a basic data structures issue? Arrays can use math to access any slot in the data structure. A linked list has to follow the links to find items. So, to access item 5 in an array: pointer = array_base + 5 * sizeof(element). A linked list must traverse through 4 nodes to get to the element; because we don't know where in memory the nodes are. – Thomas Matthews May 26 '22 at 22:30

0 Answers0