C + + Primer notes--containers

Source: Internet
Author: User

1. A number of sequential containers are defined in the standard library, and all sequential containers provide the ability to quickly sequentially access elements.

2. If the container's element type does not have a default constructor, you cannot specify the number of the container when constructing the container, because there is no way to construct the elements by default.

3. Some common container operations

4. When a container initializes another copy of the container, the element types of the two containers must match exactly, if the other container is initialized with one of the container's iterators, as long as the element type can be converted.

5. When defining an array to define its size in addition to the specified type, the array can be assigned a copy.

std::array<int3> arr = {123};std::array<int  3>:: Iterator Iter;std::array<int3> arr1 = arr;

6. Assignment operation of the container.

7.assign (array not available) can assign values to different types but compatible containers.

STD::LIST<STD::string> names;std::vector<constchar*>= Oldstyle;        // error, type different names.assign (Oldstyle.cbegin (), Oldstyle.cend ());

8. Swap does not copy, delete, or insert any elements except array, so it is guaranteed to complete in constant time.

    • An element that is not moved means that, in addition to a string, an iterator to the container, the reference and the pointer are not invalidated after the swap operation, but the container already pointed to has changed with the element.
    • Calling swap on a string causes iterators, references, and pointers to be invalidated.
    • Swap two arrays will actually swap their elements, so the time required to exchange two arrays is proportional to the number of elements in the array, and the elements bound by pointers, references, and iterators remain unchanged, but the values of the elements change.

9. The operands on either side of the relational operator must be containers of the same type, and elements of the same type must be saved. And comparisons can be made only when the element type defines the corresponding comparison operator, similar to string.

10. In addition to array, all standard library containers provide flexible memory management.

11. When we initialize a container with an object, or insert an object into a container, the object that is actually put into the container is worth a copy.

12. Under the new standard, the Insert version that accepts the number or range of elements returns an iterator that points to the first newly added element, and if the range is empty, no element is inserted, and the insert operation returns the first parameter.

std::list<int> ls; int  = ls.begin ();  while (Std::cin >> i)     = Ls.insert (iter, i);    // equivalent to calling Push_front

The 13.emplace function constructs the element directly in the container, and the arguments passed to the Emplace function must match the constructor of the element type.

14. The operation of accessing an element in a sequential container is as follows, and the following operation returns a reference, and if the container is a const object, the return value is a const reference.

15. Delete Operations for sequential containers.

16. The addition or deletion of elements in a forward_list is done by altering the elements after the given element, so forward_list does not have insert,emplace and erase, but rather defines some other operations.

17. In addition to the array, you can use resize to increase or decrease the container. If the current size is greater than the required size, the excess is removed and the new element is added to the tail.

18. Adding or removing elements to a container may invalidate pointers, references, or iterators that point to container elements.

After adding elements to the container:

    • If the container is a string or vector, and the storage space is reassigned, it will fail, and if it is not reassigned, it will still be valid until the insertion position is invalidated after the insertion position.
    • For deque, any position inserted beyond the end-to-end position is invalidated, and if an element is added at the end-to-end position, the iterator is invalidated, but pointers and references to elements that exist are not invalidated.
    • For list and forward_list, still valid.

After removing an element from the container:

    • For vector and string, points that precede the deleted element are still valid. Note: The post-tail iterator is always invalidated.
    • For deque, if an element is removed from anywhere other than the end-to-end position, pointing to the other elements outside the deleted element is invalidated, and if the tail element is deleted, the tail iterator is invalidated, but the others are unaffected. If you delete the first element, it will not be affected
    • For list and forward_list, still valid.

19. The range for statement body should not change the size of its traversed sequence, because once an element is added or removed, the value of the end () function used to determine the ending may become invalid.

The storage of 20.vector elements is continuous, and if there is not enough contiguous memory space when adding elements, the space will be redistributed. strings and vectors usually reserve space for preparation. It is not necessary to set the size of the vector object when it is defined, in fact it may be worse. There is only one exception, that is, the values of all elements are the same.

21. member functions that manage capacity.

The reserve allocates at least the space of the specified size, but it may be larger, and the reserve does nothing when the parameter is less than the current capacity.

Shrink_to_fit is only a request, the standard library does not guarantee the return of memory.

The memory is redistributed only when size () and capacity () are equal or when the insert operation is called, and how much is allocated depends on the implementation.

22. Modify the operation of the string.

23.string of search operations.

C + + Primer notes--containers

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.