C++Primer notes for the use of the order of containers

  • 2020-04-01 23:28:36
  • OfStack

A sequential container, which aggregates a single type of element into a container and then stores and accesses those elements by location, is a sequential container. There are three types defined in the standard library: vector(for fast random access), list(for fast insertion and deletion), and deque(for double-ended queues). The container defines only a few operations, and most of the additional operations are provided by the library. Type constraints on elements in the container; 1. Element type must support assignment operation; 2. Objects of the element type must be replicable. This is a minimum requirement for container element types and must be relevant if you want to support some other special requirements.

A container vector for a container can be defined < The vector < int > > Lines; // must be used" > > "Space in the middle, otherwise there will be a mutation error

Iterator operation:

The relational operators apply only to vector and deque containers, which can directly and effectively access the specified container elements based on the element location. The list container iterator supports neither arithmetic operations (addition or subtraction) nor relational operations ( < =, < . > =, > ), which provides only pre - and postposition self - increment, self - subtract, and equality (inequality) operations.

Iterator scope:

C++ USES a pair of iterators to mark the iterator scope, usually named first and last or beg and end. The elements in this range include the elements to which the iterator first points, and all the elements from first to the position to which the iterator last points. Such an element range is called the left closed interval [first,last].

Operation of sequential containers:

Add elements to the container; Delete elements in the container; Set the container size; Gets the first and last elements in the container. Const versions are available for begin(), end(), rbegin(), and rend(). If the container is const, its return type is prefixed with const_. C.push_back (t) adds an element of value t to the end of container c, returning type void. C. ush_front(t) adds an element with value t to the front of container c, returns type void. But only list and deque have this property.


//Order traversal of containers
vector<int>::reverse_iterator iterReverse=vect.rbegin();//Define the reverse iterator while(iterReverse! = vect. Rend ())
{
    cout<<*iterReverse<<endl;
    iterReverse++;
}
vector<int>::iterator iter = vect.begin();//Define a forward iterator
while(iter!=vect.end())
{
    cout<<*iter<<endl;
    iter++;
}

Add an element at the specified location in the container: use the insert function: since the iterator may point to the next location beyond the end of the container, which does not exist, the insert function inserts an element before it points to the location rather than after it. Mylist. Insert (iter, element);

//To get the central location iterator, note that the list does not allow the addition of the following iterator
vector<int>::iterator middle=vectCpy.begin()+vect.size()/2;
vectCpy.insert(middle,1001);//Add an element in the middle & NBSP;

Insert an element:

vectCpy.insert(vectCpy.begin(),10,9);//Add 10 elements with an initial value of 9 after the first element
int num[3]={555,666,777};
vectCpy.insert(vectCpy.end(),num,num+3);//VectCpy is followed by an element from the num array
vectCpy.insert(vectCpy.end(),vect.begin(),vect.end());//After vectCpy, add an element from the iterator pair

It is important to note that adding elements can cause some or all iterators to fail, and it is safest to assume that all iterators fail. Do not store the iterator returned by the end operation. To avoid storing the end iterator, you can recalculate the value of the end iterator after each insertion operation.

Key concept: container elements are copies when adding elements to a container, the system copies the values of the elements into the container. Similarly, when an element is used to initialize a new container, the new container holds a copy of the original element. The original value of this copy is independent of the elements in the new container, so that the copied original value will not be affected when the values of the elements in the container change, and vice versa.

Container comparison:

The containers being compared must have the same container type and their element types must be the same. Container comparisons are based on comparisons of elements within containers. If two containers have the same length and all the elements are equal, then these two containers are equal. A shorter container is said to be less than another container if two containers have different lengths, but all the elements in the shorter container are equal to their counterparts in the longer container. If both containers are an initial subsequence of each other, the result of their comparison depends on the first unequal element being compared.


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 elements at the back of the container are deleted, and if the current container length is less than the new length value, the system adds new elements at the back of the container. The resize operation may invalidate the iterator. Example:


list<int> ilist(10,2);//10 element containers, all with an initial value of 2
ilist.resize(15);//Add 5 elements to the original, starting at 0
ilist.resize(25,-1);//Add 10 more elements to the upstream, and the value is -1
ilist.resize(5);//Delete 20 elements at the back of ilist

Access elements:

If the container is not empty, the front and back members of the container type return references to the first or last element in the container


int &ref=vect.front();//Front and back return a reference to the first or last element in the container
ref=1000001;//Changing the referenced element changes the value of the element within vect
cout<<vect[0]<<" "<<vect.at(1)<<endl;//It only applies to vector and deque. If the index given is invalid, the exception of outOfRange will occur

Delete elements:

The pop_front and pop_back functions are used to delete the first and last elements in the container.

A more common way to delete one or more elements is to use the erase operation, which has two versions: delete a single element pointed to by an iterator, or delete a segment marked by a pair of iterators. Erase returns an iterator that points to the deleted element or the element following the element segment. Typically, the erase operation must be used after the element to be deleted is found in the container. The easiest way to find a specified element is to use the find method in the standard library. The header file algorithm.h must be included


#include<algorithm>
list<int>::iterator searchIter= find(mylist.begin(),mylist.end(),1233);
if(searchIter!=mylist.end())//Probably not
{
mylist.erase(searchIter);
}

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

Selection of containers:

The vector and deque containers provide fast random access to elements, but at the cost of more overhead to insert or delete elements anywhere in the container than to insert and delete elements at the end of the container. List types can be quickly inserted and deleted anywhere, but at the expense of random access to elements. The reason for this is that in an internally implemented data structure, one is in memory sequential address batching, and the other is in a linked list-like manner, random address allocation, so the nature of the difference.


Related articles: