Why do you use an editor, not an index?

Asked 2 years ago, Updated 2 years ago, 99 views

//code1
for (int i = 0; i < some_vector.size(); i++)
{
    //...
}

//Code2
for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();
    some_iterator++)
{
    //...
}

Usually, when you use a container, you use the second code a lot Why is that?

c++ stl iterator

2022-09-21 19:21

1 Answers

Code 1 is more efficient when container.size() is faster. However, container.size() is fast only when the container is vector Other containers such as list or map are written like code 2 because this operation is slow.

Code 1 must be accessed within the repeat statement, such as Telem=some_vector[i];. For this to be possible, the container must support operator[](std::size_t) This is also not supported by other container types except vector.


2022-09-21 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.