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