The difference of vector, list and deque in STL

Source: Internet
Author: User
Tags data structures

1VectorThe vector corresponds to an array
Allocates a contiguous memory space in memory for storage. Supports storage that does not specify a vector size. STL internal implementation, first allocate a very large memory space to store, that is, the size of the Capacituy () function return, when more than this allocated space to redistribute a piece of memory storage, this gives a vector can not specify a vector that is the size of a contiguous memory. Typically, this default memory allocation completes most of the storage.
Advantages: (1) No contiguous storage of an array of memory size is specified, that is, it can be manipulated like an array, but the array can be
For dynamic operation. Usually embodied in push_back () Pop_back ()
(2) Convenience of random access, that is, support [] operator and vector.at ()
(3) Space saving.
Disadvantages: (1) The internal insertion delete operation is inefficient.
(2) Push and pop can only be done at the end of the vector and cannot be pushed and pop on the Vector's head.
(3) When the dynamically added data exceeds the vector default allocation size, the overall redistribution, copying and interpretation
Put

2List
Two-way linked list
Each node includes a info Quick info, a precursor pointer pre, and a rear-drive pointer post. You can add and remove operations without allocating the required memory size. is stored in a non contiguous memory space.
Advantages: (1) Do not use continuous memory to complete dynamic operation.
(2) Easy to insert and delete within the internal operation
(3) can be push at both ends, pop
Disadvantages: (1) cannot carry on the internal random access, namely does not support [] the operator and the vector.at ()
(2) compared with the verctor to occupy more memory

3deque
Double-end queue with two-terminal queues
The deque incorporates vectors and lists on a functional list.
Advantages: (1) Random access convenience, that is, support [] operator and vector.at ()
(2) Easy to insert and delete within the internal operation
(3) can be push at both ends, pop
Disadvantages: (1) occupy more memory

Use the difference:

1 If you need efficient and immediate access without caring about the efficiency of insertions and deletions, use vector
2 If you need a lot of insertions and deletions without concern for immediate access, you should use the list
3 If you need immediate access and care about inserting and deleting data at both ends, you should use the Deque





The use of vector containers in C++stl

http://xiamaogeng.blog.163.com/blog/static/1670023742010102494039234/

Vector is part of the C + + Standard Template Library, a versatile template class and function library that can manipulate a variety of data structures and algorithms. The vector is considered a container because it can hold various types of objects like a container, simply saying that vector is a dynamic array of any type that can add and compress data. In order to be able to use vectors, you must include the following code in your header file:

#include <vector>

Vectors belong to the STD-named domain, so you need to have a naming qualification to complete your code as follows:

Using Std::vector; Vector<int> v;

Or even together, using the full name:

Std::vector<int> v;

It is recommended that you use the global named Domain method:

using namespace Std;

1.vector's statement

Vector<elemtype> C; To create an empty vector

Vector<elemtype> C1 (C2); Create a vector c1 and use C2 to initialize C1

Vector<elemtype> c (n); Create a vector containing n elemtype types of data;

Vector<elemtype> c (N,elem); Create a vector containing n elemtype types of data, all initialized to Elem;

C.~vector<elemtype> (); Destroy all data and release resources;

The functions commonly used in 2.vector containers. (c is a container object)

C.push_back (Elem); Add an element to the last position of the container Elem

C.pop_back (); Delete the element at the last position of the container

c.at (index); Returns the element at the specified index position

C.begin (); Returns a pointer to the data at the beginning of the container

C.end (); Returns a pointer to the last data unit of a container +1

C.front (); Returns a reference to the container's first cell data

C.back (); Returns a reference to the last data in a container

C.max_size (); Returns the maximum capacity of a container

C.size (); Returns the actual number of elements stored in the current container

C.capacity (); With C.size ()

C.resize (); To reset the capacity of a vector

C.reserve (); With C.resize ()

C.erase (P); Deletes the data pointing to the position by the pointer p, and returns a pointer to the next data position (iterator)

C.erase (begin,end) deletes data from the Begin,end interval and returns a pointer to the next data position (iterator)

C.clear (); Clear All data

C.rbegin (); Returns the start pointer after the vector reversal (actually the original end-1)

C.rend (); Returns the end pointer after the vector reversal (actually the original begin-1)

C.empty (); Evaluates whether the container is empty, returns true if it is null, or returns false

C1.swap (C2); Swap data in two containers

C.insert (P,elem); Inserts a data elem at the location pointed to by the pointer P, returning a pointer to the Elem position

C.insert (P,n,elem); Insert n elem data at position p, no return value

C.insert (p,begin,end) inserts the data in the interval [begin,end) at position p, no return value

Operation in 3.vector

Operator[] such as: c.[i];

The same as the at () function, which takes the data from the container.

The functions and operations contained in the Vector class are described in general, and the following continues to discuss how to use vector containers;

1. Input and deletion of data. Push_back () and Pop_back ()


2. Access to elements


3. Sorting and querying


4. Two-D container



C + + STL List Queue usage (instance) http://www.cnblogs.com/madlas/articles/1364503.html

C + + STL List Queue usage (instance) 2007-12-15 12:54

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>

using namespace Std;

Create an instance of a list container listint
typedef list<int> Listint;

Create an instance of a list container Listchar
typedef list<char> LISTCHAR;

void Main (void)
{
//--------------------------
Handling integer data with the list container
//--------------------------
Create a list object named Listone with Listint
Listint Listone;
Declaring I as an iterator
Listint::iterator i;

Adding data to the Listone container in the past
Listone.push_front (2);
Listone.push_front (1);

Add data to the Listone container from the back
Listone.push_back (3);
Listone.push_back (4);

Display data from Listone in front of back
cout<< "Listone.begin ()---listone.end ():" <<endl;
for (i = Listone.begin (); I!= listone.end (); ++i)
cout << *i << "";
cout << Endl;

Display data from Listone in the back
Listint::reverse_iterator ir;
cout<< "Listone.rbegin ()---listone.rend ():" <<endl;
For (IR =listone.rbegin (); Ir!=listone.rend (); ir++) {
cout << *ir << "";
}
cout << Endl;

Accumulate (additive) algorithm using STL
int result = accumulate (Listone.begin (), Listone.end (), 0);
cout<< "sum=" <<result<<endl;
cout<< "------------------" <<endl;

//--------------------------
Handling character data with the list container
//--------------------------

Create a list object named Listone with Listchar
Listchar Listtwo;
Declaring I as an iterator
Listchar::iterator J;

Adding data to the Listtwo container in the past
Listtwo.push_front (' A ');
Listtwo.push_front (' B ');

Add data to the Listtwo container from the back
Listtwo.push_back (' x ');
Listtwo.push_back (' Y ');

Display data from Listtwo in front of back
cout<< "Listtwo.begin ()---listtwo.end ():" <<endl;
for (j = Listtwo.begin (); J!= Listtwo.end (); ++j)
cout << char (*j) << "";
cout << Endl;

Using STL max_element algorithm to find the largest element in Listtwo and display
J=max_element (Listtwo.begin (), Listtwo.end ());
cout << "The maximum element in Listtwo is:" <<char (*J) <<endl;
}

#include <iostream>
#include <list>

using namespace Std;
typedef list<int> Intlist;

Show all elements of the list queue back to front
void Put_list (intlist list, char *name)
{
Intlist::iterator plist;

cout << "The contents of" << name << ":";
for (plist = List.begin (); plist!= list.end (); plist++)
cout << *plist << "";
cout<<endl;
}

Testing the function of the list container
void Main (void)
{
List1 object is initially empty
Intlist List1;
The List2 object initially has 10 elements with a value of 6
Intlist List2 (10,6);
The List3 object initially has 3 elements with a value of 6
Intlist List3 (List2.begin (),--list2.end ());

Declares a bidirectional iterator named I
Intlist::iterator i;

To display the elements of each list object in front of the back
Put_list (List1, "List1");
Put_list (List2, "List2");
Put_list (List3, "list3");

Add two elements after the list1 sequence
List1.push_back (2);
List1.push_back (4);
cout<< "List1.push_back (2) and List1.push_back (4):" <<endl;
Put_list (List1, "List1");

Add two elements from the front of the list1 sequence
List1.push_front (5);
List1.push_front (7);
cout<< "List1.push_front (5) and List1.push_front (7):" <<endl;
Put_list (List1, "List1");

Inserting data in the middle of a list1 sequence
List1.insert (++list1.begin (), 3,9);
cout<< "List1.insert (List1.begin () +1,3,9):" <<endl;
Put_list (List1, "List1");

Test Reference class function
cout<< "list1.front () =" <<list1.front () <<endl;
cout<< "list1.back () =" <<list1.back () <<endl;

Remove an element from the front and back of the list1 sequence
List1.pop_front ();
List1.pop_back ();
cout<< "List1.pop_front () and List1.pop_back ():" <<endl;
Put_list (List1, "List1");

Clear the 2nd element in the List1
List1.erase (++list1.begin ());
cout<< "List1.erase (++list1.begin ()):" <<endl;
Put_list (List1, "List1");

Assign values to List2 and display
List2.assign (8,1);
cout<< "List2.assign (8,1):" <<endl;
Put_list (List2, "List2");

Show status information for a sequence
cout<< "List1.max_size ():" <<list1.max_size () <<endl;
cout<< "List1.size ():" <<list1.size () <<endl;
cout<< "List1.empty ():" <<list1.empty () <<endl;

Operation of List sequence container
Put_list (List1, "List1");
Put_list (List3, "list3");
cout<< "LIST1>LIST3:" << (LIST1>LIST3) <<endl;
cout<< "LIST1<LIST3:" << (LIST1<LIST3) <<endl;

Sort the List1 container
List1.sort ();
Put_list (List1, "List1");

Combined processing
List1.splice (++list1.begin (), LIST3);
Put_list (List1, "List1");
Put_list (List3, "list3");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.