Learn the personal understanding of C + + Primer (ix)

Source: Internet
Author: User
Tags list of attributes

This chapter introduces the sequential containers, in the previous chapter, the learned vectors belong to one of the sequential containers.

a container is a collection of certain types of objects .

In addition to vectors, what are the sequential containers?

Vector

    Variable size, random access is fast, but inserting or deleting elements outside the trailer can be slow.

  Deque:

    The speed of random access is fast, and the insertion or deletion of headers and tails is fast.

  List

two-way linked list, only support bidirectional sequential access, insertion or deletion at any location is quick (List of attributes)

  Forward_list:

one-way linked list, only one-way random access is supported. Insertion or deletion at any location is quick

  Array:

    Fixed-size arrays, cannot add or remove elements

  String

    Vector-like but specifically used to hold characters.

string and vector store elements in contiguous memory space. So the use of subscript to calculate the address is very fast.

! However, inserting or deleting between these two containers can be slow because of the continuous problem of memory space, after each insertion or deletion, the subsequent elements are moved accordingly.

List and Forward_list

  It is designed to be quick to add or remove containers in any location. However, random access to elements is not supported.

In order to access elements, only the entire container can be traversed . The additional memory overhead for these two containers is also significant compared to the vector deque and array.

The forward_list is designed to achieve the same performance as a handwritten one-way list, and does not support the size operation because it can incur additional overhead.

Deque

  Similar to string and vector, but adding or removing elements at both ends of Deque is quick.

Array

  Similar to built-in arrays, the size of an array object is fixed. Array does not support adding and removing elements, and resizing operations.

The appropriate sequential container should be selected

  This book says that the new version of the container is much faster than the old version, C + + programs should use standard library containers as much as possible.

But how should we choose the right order container?

  As long as you remember the characteristics of each container, try to choose the fastest.

List and forward_list are linked lists, insertions and deletions are quick for any location, because the elements of the linked list only need to point to the memory address of the next element. There is no need to move all the way around, which means finding an element in a linked list is cumbersome.

Vector and deque randomly accessed elements quickly, deque inserted or deleted at the front end quickly.

After learning some generic algorithms in later chapters, you can actually use these containers more flexibly. Without being too obsessed with its characteristics.

  

Operation of the container

  This part is not difficult in fact, especially after understanding the following generic algorithm.

The book says:

Some operations are supported by all containers.

Second, sequential container association containers unordered containers have some unique operations

There are also some operations that apply only to a small number of containers

Containers are defined as template classes. So we need to provide additional information to generate the container type.

  

Iterators

  Iterators have a common interface, and if an iterator provides an operation, then all iterators that provide the same operation will implement the same operation in the same way.

  

An iterator range is represented by a pair of iterators. They mark a range of elements in the container.

  

  Programming assumptions contained in the left closed range

    Assuming that begin and end form a valid iterator range:

1. If begin = = end, the range is empty.

2. If begin! = end, there is at least one element within the range, and begin points to the first element

3. I can increment the begin several times so that begin = = End

  Container type Members

    Chapter III has been introduced, in order to use these types, you must explicitly use their class name, such as:

Lise<string>:: Iterator iter;

  Begin and End members

    The version prefixed with R returns a reverse iterator, and the version beginning with C returns a const iterator.

  

  How containers are defined and initialized

    In addition to the array, each container defines the default construction parameters.

    There are two ways to create a new container as a copy of another container:

      1. Copy the entire container directly (requires exactly the same type)

2. Use iterators to specify the range (not the same, even if the element type is different)

  Standard library array has a fixed size

    The size of the array is also part of the type. Such as:

Array<int,20>

Array<int,20>::size_type

  Assignment and Swap

    C1 = C2//Replace the contents of C1 with copies of elements in C2

C1 = {A,b,c}//Assigned C1 size is 3

The standard library array is different from the built-in array, allowing assignment

    An array can be initialized with a curly brace list, but it cannot be assigned a value with a curly brace list.

Using assign (Sequential container)

    In the sequential container, in addition to array, a member named assign is defined. allows us to assign values from a different but compatible type.

For example:

      List<string> names;

Vector<const char*> Oldstyle;

names = Oldstyle; This will cause an error because the container is of different type and the element type is different.

Names.assign (Oldstyle.cbegin (), Oldstyle.cend ());

      Names uses member assign to replace elements in names with copies of elements in the specified range of iterators.

     

Using swap

    Swap operation swaps the contents of two containers of the same type.

    In addition to array, the operation of exchanging two container contents is very fast. because: * * The element itself is not exchanged, only the internal data structure of the container is exchanged.

In addition to string, calling swap does not cause iterators, references, or pointers to fail.

C++11 the new standard, swap is divided into member functions and non-member functions, we recommend the use of non-member functions.

Container size operation

Size is not supported except for forward_list. All others support Size,empty, and max_size

  

Sequential container operations

First, inserting an element into a vector,string or deque invalidates all iterators, pointers, and references that point to the container.

Using Push_back

    Push_back is supported in addition to array and forward_list

 Using Push_front

    List, forward_list, deque supports inserting elements into the container header. (using Push_front)

 Add an element at a specific location in a container

    Insert members provide added functionality that allows us to add elements anywhere in the container.

Inserts are supported in addition to array operations including string. Forward_list has its own special version of insert

    Insert accepts an iterator as the first parameter. The iterator indicates where to place the new element in the container.

The Insert function inserts the element before the position referred to by the iterator.

    It is important to note that although some containers do not support Push_front, you can use Insert and pass the begin iterator in.

    * Try not to insert elements in the vector string deque, although this is legal.

Inserting elements in a range

Insert can accept more parameters, such as:

    Svec.insert (Svec.end (), ten, "Anna"); Insert 10 "Anna" at the end of the container

Svec.insert (Svec.begin (), A.begin (), A.end ()); Inserts an element in the range of a container iterator at the start position

   

    Use the return value of insert

      The returned iterator that is called after insert points to the location of the new element.

    Using the emplace operation

      Divided into Emplace_front,emplac,emplac_back

The function of using the Emplace series means that instead of passing an object, we can pass directly the parameters needed to construct the object.

      But the arguments passed to Emplace must match the construction of the element type

accessing elements

  

Learn the personal understanding of C + + Primer (ix)

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.