How to Use the vector container in C/C ++ (second bullet)

Source: Internet
Author: User
Tags bit set

This article summarizes the commonly used vector operations, which are the continuation of the previous article! Only the code is available. For details, see the comments in the code. For anti-crawler purposes, you may not find it on http://blog.csdn.net/zhanh1218.

/*************************************** * ****************************** File_name: vector_test.cpp ** Created on: June 28, 2014 3:34:23 * Author: The_Third_Wave, Blog: http://blog.csdn.net/zhanh1218 * Email: zhanh121823@sina.com * Last modified: june 28, 2014 3:34:23 ************************************* * *******************************/# include
 
  
# Include
  
   
# Include "Headers/Myfunc. h" # include "Headers/Person. h" void output (const std: vector
   
    
& Vec) // because the parameter is output rather than modified, it is defined as a constant reference to improve the reliability and efficiency (&)! {Std: cout <"size:" <vec. size () <", capacity:" <
    
     
Vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; pr_vector (vec); // access the first element, vec of the native method. front () and vec. before using back (), it is generally best to perform Element check vec. size () std: cout <vec. front () <std: ends <vec. back () <std: endl; // use the iterator [NOTE: * (-- vec. end (). It is recommended that you perform Element check vec before use. size () std: cout <* vec. begin () <std: ends <* (-- vec. end () <std: endl; auto a = vec. size (); // Let the compiler automatically analyze the type of the expression auto B = vec. capacity (); // The number of elements that the vector can save when memory is not re-allocated. std: cout <"size:" <a <std :: ends <"capacity:" <
     Vec2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // reinitialize pr_vector (vec); pr_vector (vec2); std:: swap (vec, vec2); // [Fast] swap (a, B) pr_vector (vec); pr_vector (vec2); vec2.assign (vec. begin (), vec. end (); // The parameter is another iterator of the same type vector pr_vector (vec); pr_vector (vec2); vec. assign ({0, 0, 0, 0, 0}); // The parameter is the initialization list pr_vector (vec); vec. assign (10, 1); // Replace with 10 1 pr_vector (vec); // The native method of vector that does not insert data into the header can only be operated through the insert () method, insert bit Set them to the first position pointed to by the pointer // There are 4 types // The first vec. insert (p, t); p is its own iterator, t is the value to be inserted, and the return value is the iterator pointing to the newly added element auto p = vec. insert (vec. begin () + vec. size ()/2, 6688); // The second vec. insert (p, n, t); p is its own iterator, insert n t, and return the iterator pointing to the first newly added element. If n is 0, returns p vec. insert (p, 3, 4); // The third vec. insert (p, B, e); p is the iterator of its own, B and e are the iterator of other vec objects of the same type, and the return value is the iterator pointing to the newly added first element. If the range is null, p vec is returned. insert (p, vec2.crbegin (), vec2.crend (); // const reverse iterator // type 4 vec. insert (p, il); p is its own iterator, il is the list of element values, and the returned value is the iterator pointing to the newly added first element. The list is empty, returns p vec. insert (vec. begin (), {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}); pr_vector (vec); // use the insert return value to implement repeated insertion at a specific position, the preceding result indicates the // emplace operation. For example, [c ++ 11] emplace_front [vector not], emplace, and emplace_back correspond to push_front [vector not], insert, push_back std: vector
      
        Per = {"The_Third_Wave", 100, "Blog: http://blog.csdn.net/zhanh1218" }}; // class initialization + vector initialization, so {{}, {}} must be per. emplace_back ("The_Third_Wave", 188, "This blog: http://blog.csdn.net/zhanh1218/article/details/35569587"); per. emplace (per. begin () + 1, Person ("The_Third_Wave", 168, "Last blog: http://blog.csdn.net/zhanh1218/article/details/33323111"); for (auto & p: per) {print (std: cout, p);} // Delete the object. The interpreter does not check whether the element exists. pop_back (), vector no pop_front () vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; vec. pop_back (); pr_vector (vec); // vec. erase (p) [Delete the element referred to by iterator p. The returned value is the iterator after the element is deleted. P points to the end and returns the end iterator. If p is the end iterator, it will kill you. Congratulations !] Vec. erase (vec. begin () + 8); pr_vector (vec); // vec. erase (B, e) [Delete the range element specified by iterator B and iterator e. The returned value is the iterator after the deleted element. If e is the post-end iterator, the returned iterator is the post-end iterator. Congratulations !] Vec. erase (vec. begin () + 3, vec. end ()-1); pr_vector (vec); // vec. clear () deletes all objects and returns void vec. clear (); pr_vector (vec); std: cout <"there are empty rows on it! Otherwise, an error occurs. ";}
      
    
   
  
 
Result:

0 1 2 3 4 5 6 7 8 9 0 90 9 size: 10 capacity: 10 size: 10, capacity: 50 size: 10, capacity: 100 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 size: 50, capacity: 500 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 size: 90, capacity: 1000 1 2 3 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1444 1521 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 4 9 16 25 36 49 6 4 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 0 1 2 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 9 8 6 5 4 3 2 1 0 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0 4 4 6688 1 1 1 1 1 1 The_Third_Wave 100 Blog: http://blog.csdn.net/zhanh1218The_Third_Wave 168 Last blog: http://blog.csdn.net/zhanh1218/article/de Tails/33323111The_Third_Wave 188 This blog: http://blog.csdn.net/zhanh1218/article/details/355695870 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 7 above empty line on the right! Otherwise, an error occurs.

This article by @ The_Third_Wave (Blog address: http://blog.csdn.net/zhanh1218) original. Otherwise, it will be updated from time to time. please correct me if any error occurs.

If you find that this Blog post is incomplete, it is the reason for me to prevent crawlers from releasing half of the posts first. Please refer to the original author's Blog.

If this blog post is helpful to you, you are not recommended to repost it for a good network environment. We recommend that you add it to your favorites! If you must reprint the content, please include the suffix and the address of this 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.