This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Whizlab question on Vector

Mohit Goyal
Ranch Hand
Posts: 65
A Vector maintains object refrences in the order they were added.
ans given is true.
According to me ans should be false because the Vector is ordered by index not by insertion order

Vishy Karl
Ranch Hand
Posts: 116
Hi there,

According to me ans should be false because the Vector is ordered by index not by insertion order

Isn't the insertion order same as ordered by the index ??
Do clarify?

Pierre Henry
Ranch Hand
Posts: 31
Mohit,
The method

adds o to the end of the vector.
The method

inserts element at position index, 0<=index<size()
This means that when you add an object to a vector , this is added at
the end, so it maintains the order.

Ranch Hand
Posts: 504
Originally posted by Mohit Goyal:
A Vector maintains object refrences in the order they were added.
ans given is true.
According to me ans should be false because the Vector is ordered by index not by insertion order

The wording sounds a bit ambiguous to me. Well, let's see how a Vector maintains order of its elements. There're 3 methods that put a new element at the end of the vector so that the resulting order is in fact the insertion order:

And there're 2 other methods that allow you to insert element(s) at the specified position:

In both cases, the elements are accessible either through their indexes or the iterator() method. If the elements were added using the first group of methods, iterator() returns them in the order they were inserted.

Mohit Goyal
Ranch Hand
Posts: 65
Thanx Vad u really cleared my doubt. i was going through Khalid's book this evening in which it was given that Vector is ordered by insertion order. But I think u r right they ordered by both