C + + Primer Fourth Edition reading notes (eight) Order container (cont.)

Source: Internet
Author: User

3.6 Accessing elements

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

Accessing the operations of elements within a sequential container
C.back () Returns a reference to the last element of container C. If C is empty, the operation is undefined
C.front () Returns a reference to the first element of container C. If C is empty, the operation is undefined
C[n] Returns a reference to an element with the subscript n
If N<0 or n>c.size (), the operation is undefined
For vector and deque containers only
C.at[n] Returns a reference to the element labeled N. If the subscript is out of bounds, the operation is undefined
For vector and deque containers only

3.7 Deleting an element

Delete an operation of an element in a sequential container
C.erase (P) Delete the element that the iterator P points to
Returns an iterator that points to the element following the deleted element. If P points to the last element within the container, the
The returned iterator points to the next position beyond the end of the container. If p itself is pointing to the next position beyond the end
Iterator, the function is undefined
C.erase (b, E) Remove all elements in the range marked by Iterators B and E
Returns an iterator that points to the element following the deleted element segment. If e itself is a pointer to the next position beyond the end
Iterator, the returned iterator also points to the next position beyond the end of the container
C.clear () Removes all elements in container C. return void
C.pop_back () Deletes the last element of container C. returns void. If C is an empty container, the function is undefined
C.pop_front () Removes the first element of container C. Returns VIOD. If C is an empty container, the function is undefined

3.7.1 Delete First or last element

The Pop_front and Pop_back functions are used to delete the first and last elements within a container. However, the vector container type does not support pop_front operations. These operations delete the specified element and return void.

The return value of the Pop_front and Pop_back functions is not a deleted element value, but void. To get the deleted element value, you must call the front or back function before deleting the element.

3.7.2 Delete an element within a container

The erase operation does not check its parameters. The programmer must ensure that the scope of the iterator or iterator used as a parameter is valid.

3.8 Assignment and Swap

The operator associated with the assignment acts on the entire container. In the swap operation, other operations can be implemented with erase and insert operations. The assignment operator first deletes the elements in the operand container, and then inserts all the elements of the right-operand container into the left container.

C + + Primer Fourth Edition reading notes (eight) Order container (cont.)

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.