C + + "vector" usage and examples

Source: Internet
Author: User

1 /***2 * Vector Basics API Review3 * 8 20184  */5 6#include <iostream>7#include <vector>8 using namespacestd;9 Ten //Print VEC vector One voidPrint (vector<int> Vec,intindex) A { -Std::cout <<"VEC"<< Index <<" = "; -      for(inti =0; I < vec.size (); i++) the     { -Std::cout <<Vec[i]; -     } -Std::cout <<Std::endl; + } -  + intMain () A { at     //Vector Initialization -vector<int> vec0 (Ten,1);//10 x 1 -vector<int> Vec1 (Vec0.begin (), Vec0.begin () +2);//assigns the first two values of a vec0 to VEC1 -vector<vector<int>> VEC2 (3, vector<int> (4));//vec2 Two-dimensional vector, containing 3 elements, each containing 4 int-type VEC vectors -  -Print (VEC0,0); inPrint (VEC1,1); -  to     //Vector Basic Operation +     BOOLFlag = Vec0.empty ();//determine if the vec0 is empty -Std::cout <<"flag ="<< Flag <<Std::endl; theVec1.clear ();//Empty VEC1 *Print (VEC1,1); $ Panax Notoginseng     //Vector::insert () -Vec0.insert (Vec0.begin (),2);//Insert 2 to the front of the VEC0 thePrint (VEC0,0);  +Vec0.insert (Vec0.begin (),3,5);//Insert 5 into VEC0 with an index value of 0-2 for each position with a length of 3 APrint (VEC0,0);  theVec0.insert (Vec0.begin (), Vec1.begin (), Vec1.end ());//Insert VEC1 in front of VEC0 +Print (VEC0,0); -  $     //vector::erase () $Vec0.erase (Vec0.begin (), Vec0.begin () +4);//Remove the first 4 bits of VEC0 -Print (VEC0,0); -  the     //Vector::swap () -Vec0.swap (VEC1);//swap the vec0 with the VEC1WuyiPrint (VEC1,1); the  -     //vector<vector<int>> WuVEC0 = vec2[0];//The vector of the No. 0 position of the VEC2 is given to VEC0, because it is the initial value, so it is 4 0 -Print (VEC0,0); About  $     //vector::p ush_back () && vector::p op_back () -Vec1.push_back (2);//Add a 2 at the tail -Print (VEC1,1); -Vec1.pop_back ();//Delete last data APrint (VEC1,1); +  the      while(true) -     { $ GetChar (); the     } the     return 0; the}

C + + "vector" usage and examples

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.