Array and vector in C ++, lambda expression, C string plus operation, new type array in C ++ (array cache), multi-element array, new Buffer

Source: Internet
Author: User

Array and vector in C ++, lambda expression, C string plus operation, new type array in C ++ (array cache), multi-element array, new Buffer
Zookeeper

  1. Use a C ++ array without memory management.

  2. Array should be careful not to overflow, because it is opened on the stack memory.

  3. Array applies to any type

    # Include

    # Include

    # Include // Standard library of C ++

    # Include // C ++ string

    # Include

    Using std: array; // static array, on the stack

    Usingstd: vector; // dynamic array, stacked

    Usingstd: string;

    // You do not need to manage the memory when using a C ++ array.

    // Array be careful not to Stack Overflow

    // Array applies to any type

    Voidmain ()

    {

    Array Myint1 = {1, 2, 3, 4, 5 };

    Array Myint2 = {11, 12, 13, 14, 15 };

    Array Myint3 = {21, 22, 23, 24, 25 };

    // Array, 3> myint = {myint1, myint2, myint3 };

    Array, 3> myint = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    For (inti = 0; I

    {

    For (intj = 0; j

    {

    Std: cout <"<

    }

    Std: cout <"\ n ";

    }

    Std: cin. get ();

    }

    1. Vector: Dynamic String Array

      # Include

      # Include

      # Include // Standard library of C ++

      # Include // C ++ string

      # Include

      Using std: array; // static array, on the stack,

      Usingstd: vector; // dynamic array, stacked,

      Usingstd: string;

      // You do not need to manage the memory when using a C ++ array.

      // Array be careful not to Stack Overflow

      // Array applies to any type

      Voidmain ()

      {

      Vector String1; // dynamic String Array

      // Can be reused

      String1.push _ back ("notepad ");

      String1.push _ back ("calc ");

      String1.push _ back ("mspaint ");

      String1.pop _ back (); // delete

      // String1.clear (); // clear

      For (inti = 0; I

      {

      System (string1 [I]. c_str ());

      }

      System ("pause ");

      }

      5. Iteration through the iterator keyword

      # Include

      # Include

      # Include // Standard library of C ++

      # Include // C ++ string

      # Include

      Using std: array; // static array, on the stack

      Usingstd: vector; // dynamic array, stacked

      Usingstd: string;

      Voidmain ()

      {

      Vector String1; // dynamic String Array

      String1.push _ back ("notepad ");

      String1.push _ back ("calc ");

      String1.push _ back ("mspaint ");

      Vector : Iteratoribegin, iend; // iterator

      Ibegin = string1.begin (); // data start point

      Iend = string1.end (); // end

      For (; ibegin! = Iend; ibegin ++)

      {

      Stringtempstr = * ibegin; // obtain the data pointed to by the pointer

      System (tempstr. c_str (); // execute the command

      }

      System ("pauese ");

      }

      6. Positive and reverse iteration Array

      # Include

      # Include

      # Include // Standard library of C ++

      # Include // C ++ string

      # Include

      Using std: array; // static array, on the stack

      Usingstd: vector; // dynamic array, stacked

      Usingstd: string;

      Voidmain ()

      {

      Array Myint = {1, 2, 3, 4, 5 };

      Array : Iteratoribegin, iend; // positive iterator

      Ibegin = myint. begin ();

      Iend = myint. end ();

      While (ibegin! = Iend)

      {

      Std: cout <* ibegin <

      Ibegin ++;

      }

      Std: cout <"----------" <

      Array : Reverse_iteratorrbegin, rend;

      Rbegin = myint. rbegin ();

      Rend = myint. rend ();

      While (rbegin! = Rend)

      {

      Std: cout <* rbegin <

      Rbegin ++;

      }

      Std: cin. get ();

      }

      7. Reverse iterator

      # Include

      # Include

      # Include // Standard library of C ++

      # Include // C ++ string

      # Include

      Using std: array; // static array, on the stack

      Usingstd: vector; // dynamic array, stacked

      Usingstd: string;

      Voidmain ()

      {

      Vector String1; // dynamic String Array

      String1.push _ back ("notepad ");

      String1.push _ back ("calc ");

      String1.push _ back ("mspaint ");

      // Reverse iterator

      Vector : Reverse_iteratorrbegin = string1.rbegin ();

      Vector : Reverse_iteratorrend = string1.rend ();

      // Rend points to the next node at the end of the data instead of data.

      // When the following method is used, only notepad and calculator are printed.

      Rend --;

      A: if (rbegin! = Rend)

      {

      System (* rend). c_str (); // execute the command

      // Rbegin ++;

      Rend --;

      GotoA;

      }

      }

      8. lambda expressions are not only applicable to array, but also to vector.

      # Include

      # Include

      # Include // algorithm lambda expression, not only applicable to array, but also to vector

      Voidmain ()

      {

      Std: vector Myvector;

      Myvector. push_back (11 );

      Myvector. push_back (22 );

      Myvector. push_back (33 );

      Myvector. push_back (3 );

      Myvector. push_back (4 );

      Myvector. push_back (5 );

      Intres = 0; // result

      // & Res directly operates on a variable. res is equivalent to the return value. x represents the parameter,

      // Each time it acts as an iterator pointing to an element, braces are the code

      Std: for_each (myvector. begin (), myvector. end (), [& res] (intx) {res + = x ;});

      Std: cout <res;

      Std: cin. get ();

      }

      The running result is:

      9. add, delete, modify, and query a vector

      # Include

      # Include

      # Include // algorithm lambda expression, not only applicable to array, but also to vector

      Voidmain ()

      {

      // Allocate 5 spaces. The default value is 0.

      Std: vector Myvector (5 );

      Myvector. push_back (1 );

      Myvector. push_back (11 );

      Myvector. push_back (111 );

      Myvector. push_back (1111 );

      Myvector. push_back (2 );

      // Pop up an element and delete the last one

      Myvector. pop_back ();

      // Insert

      Myvector. insert (myvector. begin () + 1,999 );

      // Based on the iterator position

      Myvector. erase (myvector. begin () + 5 );

      // Myvector. clear (); // delete all elements

      For (inti = 0; I

      {

      If (1)

      {

      // Query and modify

      }

      Std: cout <myvector. at (I) <std: endl;

      }

      System ("pause ");

      }

      10. The element in the vector is also a vector.

      # Include

      # Include

      # Include // algorithm lambda expression, not only applicable to array, but also to vector

      Voidmain ()

      {

      // Supports dynamic and rule-free array Management

      Std: vector Myvector1;

      Myvector1.push _ back (12 );

      Myvector1.push _ back (13 );

      Myvector1.push _ back (14 );

      Std: vector Myvector2;

      Myvector2.push _ back (22 );

      Std: vector Myvector3;

      Myvector3.push _ back (32 );

      Myvector3.push _ back (37 );

      Std: vector > Allvecor;

      Allvecor. push_back (myvector1 );

      Allvecor. push_back (myvector2 );

      Allvecor. push_back (myvector3 );

      For (inti = 0; I

      {

      For (intj = 0; j

      {

      Std: cout <"<

      }

      Std: cout <"\ n ";

      }

      Std: cin. get ();

      }

      11. arrays in C ++ can be directly assigned values.

      # Include

      # Include

      # Include

      # Include

      Voidmain ()

      {

      Doubledb [4] = {1.1, 2.2, 3.3, 4.4 };

      // Std: array data type, double element type, 4 numbers

      Std: array Dbnew1 = {10.1, 10.2, 10.3, 10.4 };

      // Complete operations between Arrays

      Std: array Dbnew2 = dbnew1;

      For (inti = 0; I <4; I ++)

      {

      Std: cout <db [I] <"" <

      }

      Std: cin. get ();

      }

      Running result:

      12. array string <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kcjxwiggfsawdupq = "left"> # include

      # Include

      # Include

      # Include

      Voidmain ()

      {

      Std: array String1 = {"calc", "notepad", "tasklist", "mspaint", "write "};

      For (inti = 0; I <5; I ++)

      {

      Std: cout <string1 [I] <std: endl;

      System (string1 [I]. c_str ());

      }

      Std: cin. get ();

      }

      12. C ++ can be added

      # Include

      # Include

      # Include

      # Include

      Voidmain ()

      {

      Std: stringstr1 = "task ";

      Std: stringstr2 = "list ";

      Std: stringstr3 = str1 + str2;

      System (str3.c _ str ());

      Std: cin. get ();

      }

      13. Advanced of new, buffer zone

      # Include

      # Include

      Constintbuf (512); // a constant integer of 512

      IntN (5); // The length of the array

      Charbuffer [buf] = {0}; // static Zone

      // P1, p3, and p5 are pointer variables in the stack, and the storage address points to the heap.

      // Manually release the memory

      // P2, p4, and p6 are used as pointer variables in the stack, and the storage address is in the static zone. Buffer zone.

      // Automatically releases the memory, which is used to allocate data that will not be reused after use.

      // Avoid Memory leakage and automatically release the memory. Sacrifices the independence of memory access,

      Usingnamespacestd;

      Voidmain ()

      {

      Double * p1, * p2;

      Std: cout <"\ n ";

      P1 = newdouble [N]; // memory allocation, size of N elements

      P2 = new (buffer) double [N]; // memory allocation in the specified region

      For (inti = 0; I

      {

      P1 [I] = p2 [I] = I + 10.8; // array Initialization

      Std: cout <"p1 =" <& p1 [I] <"" <

      Std: cout <"p2 =" <& p2 [I] <"" <

      }

      Double * p3, * p4;

      Std: cout <"\ n ";

      P3 = newdouble [N]; // memory allocation, size of N elements

      P4 = new (buffer) double [N]; // memory allocation in the specified region

      For (inti = 0; I

      {

      P3 [I] = p4 [I] = I + 10.8; // array Initialization

      Std: cout <"p3 =" <& p3 [I] <"" <

      Std: cout <"p4 =" <& p4 [I] <"" <

      }

      Double * p5, * p6;

      Std: cout <"\ n ";

      P5 = newdouble [N]; // memory allocation, size of N elements

      P6 = new (buffer) double [N]; // memory allocation in the specified region

      For (inti = 0; I

      {

      P6 [I] = p5 [I] = I + 10.8; // initialize the Array

      Std: cout <"p5 =" <& p5 [I] <"" <

      Std: cout <"p6 =" <& p6 [I] <"" <

      }

      Std: cin. get ();

      }

      14. Multivariate Array

      # Include

      # Include

      // Void means that the parameter is null inside the parameter. If it is not written, it means it is null.

      Voidmain (void)

      {

      Intint1 = 10;

      Doubledouble1 = 99.8;

      Charch = 'a ';

      Char * str = "hellochina ";

      Std: tuple Mytuple (int1, double1, ch, str );

      Constintnum = 3;

      Autodata0 = std: get <0> (mytuple );

      Autodata1 = std: get <1> (mytuple );

      Autodata2 = std: get <2> (mytuple );

      Autodata3 = std: get (Mytuple); // The subscript can only be a constant

      Std: cout <typeid (data3). name () <std: endl;

      Decltype (data0) dataA; // obtain the data type and create it again

      // Mytuple. swap (mytuple); array vector has the function of Switching

      Std: cout <data0 <"" <

      Std: cin. get ();

      }

      // Tuple must be a static Array

      // Match with vector and array

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.