Original: http://genwoxuevc.blog.51cto.com/1852984/503337
C + + interview questions: What is the difference between list and vector?
Test Center: Understanding the difference between list and vector
Occurrence frequency: ★★★★
Analytical:
Vector and array similar, it has a contiguous memory space, and the starting address is unchanged, so it can very well support random access (that is, using the [] operator to access the elements), but because its memory space is continuous, so in the middle of the insert and delete will cause a copy of the memory block (the complexity is O (n) In addition, when there is not enough memory space behind the array, you need to re-request a chunk of memory that is large enough to make a copy of the memory. These have greatly affected the efficiency of vectors.
A list is implemented by a doubly linked list in a data structure, so its memory space can be discontinuous. Therefore, the data can only be accessed through pointers, which makes its random access variable very inefficient, the need to traverse intermediate elements, search complexity O (n), so it does not provide overloading of the [] operator. However, due to the characteristics of the list, it can be very efficient to support the deletion and insertion anywhere.
Because of these differences between list and vector, there are some differences between List::iterator and Vector::iterator. Take a look at the following example:
#include <iostream>
#include <vector>
#include <list>
using namespace Std;
int main (void)
{
Vector<int> v;
List<int> l;
for (int i=0; i<8; i++)//Add elements to V and L respectively
{
V.push_back (i);
L.push_back (i);
}
cout << v[2] = "<< v[2" << Endl;
cout << l[2] = "<< l[2" << Endl; Compile error, list not overloaded []
cout << (V.begin () < V.end ()) << Endl;
cout << (L.begin () < L.end ()) << Endl; Compile error, List::iterator not overloaded < or >
cout << * (V.begin () + 1) << Endl;
Vector<int>::iterator ITV = V.begin ();
List<int>::iterator ITL = L.begin ();
ITV = ITV + 2;
ITL = ITL + 2; Compile error, List::iterator no reload +
itl++;itl++; The list::iterator is overloaded with + + and can only be accessed iteratively using + +.
cout << *itv << Endl;
cout << *itl << Endl;
return 0;
}
Because Vector has a contiguous memory space and can support random access very well, Vector<int>::iterator supports "+", "+ =", "<" operators.
And the list of memory space can be discontinuous, it does not support random access, so list<int>::iterator does not support "+", "+ =", "<" and other operators, so code 20, 26 rows have compile errors. You can only use "+ +" to iterate, such as code 27 lines, using two times itl++ to move the ITL. There is also a list that does not support the [] operator, so there is a compilation error in code 18 line.
In short, if you need efficient and immediate access, instead of the efficiency of insertion and deletion, use vectors, if you need a lot of insertions and deletions, and you don't care about access, you should use list.
Answer:
Vector has a contiguous memory space, so it supports random access, and if you need efficient immediate access, instead of the efficiency of insertions and deletions, use vectors.
The list has a discrete memory space, so it supports random access, and if you need a lot of insertions and deletions, and you don't care about the immediate access, you should use list.
C + + interview questions: What is the difference between list and vector?