array arrays and vector arrays in C + +

Source: Internet
Author: User

I think the experiment will remember the relatively solid, the words are not more directly on the code.

The following is an array of arrays, which doesn't feel much.

//CPP style Arrays array#include <iostream>#include<array>#include<vector>using namespacestd;intMain () {array<int,6> Myint = {1,2, the, $,0, -2};  for(inti =0; I < myint.size (); i++)//size Gets the length, and the vector also gets the length of thecout << Myint[i] <<"  "<< (void*) &myint[i] <<Endl; Array<int,5> a1 = {1,2,3,4,5}; Array<int,5> a2 = {0, -1, -2, -3, -5}; Array<int,5> a3 = {8,9,Ten, One, A}; Array<array<int,5>,3> A ={A1, a2, A3};  for(inti =0; I < a.size (); i++)    {         for(intj =0; J < a[0].size (); J + +) cout<< A[i][j] <<" "; cout<<Endl; } cout<<Endl;  for(Auto i:a)//c++11 Syntax    {         for(auto j:i) cout<< J <<" "; cout<<Endl; } cout<<Endl;}

The following is a vector array that feels quite powerful.

Some basic operation functions are also push_back () trailing inserts, Pop_back () trailing delete, size () Gets the sizes, erase () specifies the location of the delete, clear () empty, insert () specified position inserted, empty () to determine if the array is Null returns TRUE, front () returns a reference to the first element, back () returns a reference to the last element, begin () returns an iterator to the first element, and end () returns an iterator to the tail element.

#include <iostream>#include<vector>using namespacestd;intmain () {vector<Double>db;//Array size variable the array on the heap is not necessarily contiguous    Doublem;  for(inti =0; I <4; i++) {cin>> m;//cannot direct cin input into DB because memory is not yet allocatedDb.push_back (m);//insert data automatically gets longercout << Db[i] <<"   "<< (void*) &db[i] <<Endl; } cout<< db[1] <<"   "<< (void*) &db[1] << Endl <<Endl; cout<< &db <<endl;//db is not a pointer     for(Auto i:db)//these I and the following IA ib .... It's all on the stack.{cout<< I <<"  "<< (void*) &i << Endl;//&i can only take the first address .} cout<< Endl <<Endl; Auto IA= Db.begin ();//StartAuto IB = Db.end ();//End     for(; IA! = IB; ia++) {cout<< *ia <<"   "; } cout<<Endl; Auto IIA= Db.rbegin ();//from the tailAuto IIB =Db.rend ();  for(; IIA! = IIB; iia++) {cout<< *iia <<" - "; }}
#include <iostream>#include<vector>using namespacestd;intmain () {vector<int>A1, A2; Vector<int>A3; A1.push_back (2); A2.push_back (3); A2.push_back (4); A2.push_back (5); A3.push_back (Ten); A3.push_back ( About); Vector<vector<int>> A ={A1, a2, A3};  for(Auto i:a) { for(auto j:i) cout<< J <<"   "; cout<<Endl; }    //multiple vectors can be implemented nested to achieve jagged multidimensional array lengths can be indeterminate//multiple array nesting can implement multidimensional arrays but length must be long}
#include <iostream>#include<vector>using namespacestd;intmain () {vector<string>str; Str.push_back ("WEL come!"); Str.push_back ("Hello"); Str.push_back (" World"); Str.push_back (" China"); Str.pop_back (); //Tail Delete a//str.clear ();//Clear     for(Auto IA = Str.begin (); IA! = Str.end (); ia++)    {        if((*ia) = ="Hello") {str.erase (IA); //begin will change after deletion//Break ;} cout<< *ia <<Endl; } str.erase (Str.begin ()+1);//Deletecout<<Endl;  for(Auto I:str)//two traversal outputs the following is another type of{cout<< I <<"    "; } cout<<Endl; Str.insert (Str.begin ()+1,"HHHH");//cannot cross-insert can be inserted within range      for(Auto I:str)//two traversal outputs the following is another type of{cout<< I <<"    "; }}

array arrays and vector arrays in C + +

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.