C++primer notes in the order of the use of the container _c language

Source: Internet
Author: User

A sequential container that aggregates a single type of element into a container and then stores and accesses the elements according to their location, which is the sequential container. Three types are defined in the standard library: vector (support for fast random access), list (support for fast insertion, deletion), deque (two-terminal queue) container only defines a small number of operations, and most of the additional operations are provided by the algorithm library. The type constraint of the element within the container; 1, the element type must support assignment operations; 2, the object of the element type must be replicable. This is the minimum requirement for a container element type, and it must have a relevant nature if you want to support some other special requirements.

Containers can be defined vector< vector<int> > lines;//must use the "> >" Space in the middle, otherwise there will be a mutation error

Iterator operations:

Relational operators apply only to vectors and deque containers, and they can directly and efficiently access the specified container element based on the location of the element. The list container's iterator does not support either arithmetic operations (addition or subtraction), nor does it support relational operations (<=,<,>=,>), which provides only the predecessors, the self subtraction operations, and the equal (unequal) operations.

Iterator scope:

C + + uses a pair of iterators to tag the iterator scope, usually named the primary and last or beg and end. The elements within the range include the elements that the iterator first points to, and all elements from the beginning until the position that the iterator last points to, which is called the left closed interval [first,last).

Operation of sequential container:

Add an element to the container, delete the element in the container, set the container size, and get the first and last elements within the container. For the Begin (), End (), Rbegin (), rend () Four actions, there is a const version. If the container is const, its return type is prefixed with the const_ prefix. C.push_back (t) adds an element with a value of t at the end of container C, and returns a void type. C.push_front (t) adds an element with a value of t at the front end of container C, and returns a void type. But only the list and the deque have such properties.

Copy Code code as follows:

Sequential traversal of containers
Vector<int>::reverse_iterator Iterreverse=vect.rbegin ()///define reverse iterator while (Iterreverse!=vect.rend ())
{
cout<<*iterreverse<<endl;
iterreverse++;
}
Vector<int>::iterator iter = Vect.begin ();//define forward iterator
while (Iter!=vect.end ())
{
cout<<*iter<<endl;
iter++;
}

To add an element at a specified location in a container: Use the Insert function: Because the iterator may point to the next position beyond the end of the container. A nonexistent element, so the Insert function is to insert the element before it points to the position instead of following it. Mylist.insert (iter,element);
Copy Code code as follows:

To get the central position iterator, the list that needs to be noted does not allow additions to the following iterators
Vector<int>::iterator Middle=vectcpy.begin () +vect.size ()/2;
Vectcpy.insert (middle,1001);//Add an element to the middle position

   To insert a section of an element:
Copy Code code as follows:

Vectcpy.insert (Vectcpy.begin (), 10,9)//Add 10 elements after the first element with an initial value of 9
int num[3]={555,666,777};
Vectcpy.insert (Vectcpy.end (), num,num+3)//Add an element from the NUM array after vectcpy
Vectcpy.insert (Vectcpy.end (), Vect.begin (), Vect.end ())//Add an element from the iterator pair after the vectcpy

It should be noted that adding elements may cause some or all iterators to fail, assuming that all iterator failures are the safest practice. Instead of storing the iterator returned by the end operation, to avoid storing the end iterator, you can recalculate the end iterator value each time the insert operation is completed.

Key concept: When a container element is a replica that adds elements to the container, the system is copying the element values into the container. Similarly, when a new container is initialized with a section of elements, a copy of the original element is stored by the new container. The original value of this copy is irrelevant to the elements in the new container, and then the copied original value will not be affected, and vice versa, when the value of the element in the container changes.

Comparison of containers:

The container for the comparison must have the same container type and must have the same element type. The comparison of containers is based on the comparison of elements within the container. Two containers have the same length and all elements are equal, then the two containers are equal. If two containers are not of equal length, but all elements in a shorter container are equal to the corresponding elements in a longer container, the shorter container is said to be smaller than the other container. If two containers are each other's initial subsequence, their comparison results depend on the first unequal element being compared.

Copy Code code as follows:

Vector<int> Vect;
Vect.push_back (1);
Vect.push_back (2);
Vect.push_back (3);
Vector<int> vectcpy (Vect);
if (vectcpy==vect) cout<< "Equal" <<endl;
else cout<< "Not Equal" <<endl;

Container size operation:

The container type provides the resize function to change the number of elements contained in the container. If the current container length is greater than the new length value, the element at the back of the container is deleted, and if the current container length is less than the new length value, the system adds a new element to the back of the container. The resize operation may invalidate the iterator. Example:

Copy Code code as follows:

list<int> IList (10,2);//10 element containers with initial values of 2
Ilist.resize (15);//On the original basis, followed by the addition of 5 elements, the initial value of 0
Ilist.resize (25,-1)//on the upstream basis, followed by the addition of 10 elements, the value is-1
Ilist.resize (5);//delete 20 elements at the back of IList

To access an element:

If the container is not empty, the front and back members of the container type return a reference to the first or last element within the container

Copy Code code as follows:

int &ref=vect.front ();//front and back returns a reference to the first or last element within the container
ref=1000001;//change the referenced element, the value of the Vect element will also change
cout<<vect[0]<< "" "<<vect.at (1) <<endl;//applies only to vectors and deque, and if the given subscript is invalid, a Outofrange exception occurs

To Delete an element:

The Pop_front and Pop_back functions are used to delete the first and last elements within a container.

The more common way to delete one or more elements is to use the erase operation, with two versions: to delete a single element that is pointed by an iterator, or to delete a section of an element marked by a pair of iterators. Erase returns an iterator that points to the element behind the deleted element or element segment. Typically, you must find the element you want to delete in the container before you use the erase action. The easiest way to find a specific element is to use the found method in the standard library. Must include header file algorithm.h

Copy Code code as follows:

#include <algorithm>
List<int>::iterator searchiter= Find (Mylist.begin (), Mylist.end (), 1233);
if (Searchiter!=mylist.end ())//may not be found
{
Mylist.erase (Searchiter);
}


Delete all elements in the container: Mylist.clear () or Mylist.erase (Mylist.begin (), Mylist.end ());

Selection of containers:

Vector and deque containers provide fast, random access to elements, but at the cost of inserting or deleting elements at any point in the container, which is more expensive than inserting and deleting elements at the end of the container. The list type can be quickly inserted and deleted at any location, but the cost of the element's random access overhead is greater. The reason is that in the internal implementation of the data structure, one is in the sequential address in memory in batches, and the other is similar to linked lists in the way, random address allocation, so that the nature of the difference.

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.