C + + sequential container vector, deque, list

Source: Internet
Author: User

1. Container element type

Most data types in C + + can be the element type of a container. The container element type must satisfy two conditions: assignment and copy operations are supported.

So no element is a container of the reference type, and neither the Io object nor the auto_ptr is the element type of the container.

2, vector container self-growth

The elements stored in the vector container are stored continuously in memory. If there is no space in the container to accommodate the new element, because the element must be stored continuously for index access, the new element cannot be stored anywhere in memory, so the vector must reallocate space for the original element and the newly added element: the elements stored in the old container are copied into the new container. , insert a new element, and finally undo the old storage space. In order for the vector container to achieve fast memory allocation, it actually allocates more capacity than is currently required, and the vector container reserves these spaces for storing new elements.

When you add an element to the list container, you only need to create a new element and then connect that element to the list that already exists, without reallocating the storage space or duplicating any existing elements.

3, Vector, deque, list three kinds of container characteristics

Vector: Supports fast random access, efficiently adding deleted data to the tail of the vector container

Deque: Supports fast random access, efficiently adding deleted data to the Deque container head and tail

List: Sequential access is supported, but inserting delete elements at any location is fast

4, vector container basic operation

  1. #include  " stdafx.h "  
  2. #include  < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< string > &NBSP;&NBSP;
  3. #include  < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< iostream > &NBSP;&NBSP;
  4. #include  < span class= "tag" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">< vector > &NBSP;&NBSP;
  5. using namespace Std;
  6. int main (int argc, char* argv[])
  7. {
  8.      //vector defines and initializes the   
  9.      vector int > Span style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">&NBSP;V1;&NBSP;&NBSP;
  10.      for  (int i = 0 ;i < 10 ;i++)   
  11. V1.push_back (i+1);
  12.      //uses iterators to traverse vector  
  13.      vector int > Span style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">::ITERATOR&NBSP; iter = v1 .begin ();   
  14. for (; Iter!=v1.end (); iter++)
  15. {
  16.          cout< < *iter < <
  17.      }  
  18.      cout< < Span class= "Tag-name" style= "margin:0px; padding:0px; Border:none; Background-color:inherit ">endl ;&NBSP;&NBSP;
  19. return 0;
  20. }

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

C + + sequential container vector, deque, list

Related Article

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.