C + + vector

Source: Internet
Author: User

A few days ago, netizens asked me the use of vector in C + +, now a simple summary of the vector, sharing csdn netizens. Description: Things are relatively simple, if you are very cow, please close this page directly!

The first is the statement of the vector:
#include <vector>
using namespace Std; In this statement
...
Vector<int> i;
...
Or
#include <vector>
...
Std::vector<int> i; Explicitly declare here

See Specific use:
1.vector of data deposit and output:

[CPP]View Plaincopy
    1. Vector<cstring> v;
    2. for (int i =0;i<40;i++)
    3. {
    4. CString s;
    5. S.format ("%d", I);
    6. V.push_back (s);
    7. }
    8. CString SS;
    9. for (Int J =0;j<v.size (); j + +)
    10. {
    11. ss+=v[j]+",";
    12. }
    13. MessageBox (ss);

Note: You can also use V.begin () and V.end () to get the pointer position of the starting and ending element addresses of the vector. You can also do this:

[CPP]View Plaincopy
    1. VECTOR<CSTRING>&NBSP;V;&NBSP;&NBSP;
    2. for (int i =0;i<40;i++)   
    3. {&NBSP;&NBSP;
    4.     cstring s;  
    5.      v.push_back (s);   
    6. }&NBSP;&NBSP;
    7. cstring ss ;  
    8. VECTOR<CSTRING>::ITERATOR&NBSP;ITER;&NBSP;&NBSP;
    9. for (  iter = v.begin ();  iter != v.end (); iter++ )    
    10. {&NBSP;&NBSP;
    11.     ss+= *iter+
    12. }&NBSP;&NBSP;
    13. MessageBox (ss);   

2. Definition of two-dimensional vector (starting with fixed length)

[CPP]View Plaincopy
  1. vector< vector<cstring> > Array (vector<cstring> (0));
  2. for ( int j = 0; J <; J + +)
  3. {
  4. For (int i = 0; i < 9; i++)
  5. {
  6. CString s;
  7. S.format ("%d", I);
  8. array[I].push_back (s);
  9. }
  10. }
  11. CString SS;
  12. for ( int jj = 0; JJ < jj++)
  13. {
  14. For (int II = 0; II < array[JJ].size (); ii++)
  15. {
  16. SS+=ARRAY[JJ][II];
  17. }
  18. }
  19. MessageBox (ss);

3. Define an array in which the rows and columns are changed

[CPP]View Plaincopy
  1. int i = 0, j = 0;
  2. vector< vector<cstring> > Array;
  3. vector< CString > line;
  4. for (j = 0; J <; J + +)
  5. {
  6. Array.push_back (line);  //To initialize each vector, the element cannot be deposited.
  7. For (i = 0; i < 9; i++)
  8. {
  9. CString s;
  10. S.format ("%d", I);
  11. array[J].push_back (s);
  12. }
  13. }
  14. CString SS;
  15. for (int JJ = 0;jj<array.size (); jj++)
  16. {
  17. For (int II =0;ii<array[jj].size (); ii++)
  18. {
  19. SS+=ARRAY[JJ][II];
  20. }
  21. }
  22. MessageBox (ss);

C + + vector

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.