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
- Vector<cstring> v;
- for (int i =0;i<40;i++)
- {
- CString s;
- S.format ("%d", I);
- V.push_back (s);
- }
- CString SS;
- for (Int J =0;j<v.size (); j + +)
- {
- ss+=v[j]+",";
- }
- 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
- VECTOR<CSTRING>&NBSP;V;&NBSP;&NBSP;
- for (int i =0;i<40;i++)
- {&NBSP;&NBSP;
- cstring s;
-
- v.push_back (s);
- }&NBSP;&NBSP;
- cstring ss ;
- VECTOR<CSTRING>::ITERATOR&NBSP;ITER;&NBSP;&NBSP;
- for ( iter = v.begin (); iter != v.end (); iter++ )
- {&NBSP;&NBSP;
- ss+= *iter+
- }&NBSP;&NBSP;
- MessageBox (ss);
2. Definition of two-dimensional vector (starting with fixed length)
[CPP]View Plaincopy
- vector< vector<cstring> > Array (vector<cstring> (0));
- for ( int j = 0; J <; J + +)
- {
- For (int i = 0; i < 9; i++)
- {
- CString s;
- S.format ("%d", I);
- array[I].push_back (s);
- }
- }
- CString SS;
- for ( int jj = 0; JJ < jj++)
- {
- For (int II = 0; II < array[JJ].size (); ii++)
- {
- SS+=ARRAY[JJ][II];
- }
- }
- MessageBox (ss);
3. Define an array in which the rows and columns are changed
[CPP]View Plaincopy
- int i = 0, j = 0;
- vector< vector<cstring> > Array;
- vector< CString > line;
- for (j = 0; J <; J + +)
- {
- Array.push_back (line); //To initialize each vector, the element cannot be deposited.
- For (i = 0; i < 9; i++)
- {
- CString s;
- S.format ("%d", I);
- array[J].push_back (s);
- }
- }
- CString SS;
- for (int JJ = 0;jj<array.size (); jj++)
- {
- For (int II =0;ii<array[jj].size (); ii++)
- {
- SS+=ARRAY[JJ][II];
- }
- }
- MessageBox (ss);
C + + vector