#include <iostream>02#include <vector>03#include <string>0405usingnamespacestd;0607main()08{09vector<string> SS;1011SS.push_back("The number is 10");12SS.push_back("The number is 20");13SS.push_back("The number is 30");1415cout <<"Loop by index:"<< endl;1617intii;18for(ii=0; ii < SS.size(); ii++)19{20cout << SS[ii] << endl;21}2223cout << endl <<"Constant Iterator:"<< endl;2425vector<string>::const_iterator cii;26for(cii=SS.begin(); cii!=SS.end(); cii++)27{28cout << *cii << endl;29}3031cout << endl <<"Reverse Iterator:"<< endl;3233vector<string>::reverse_iterator rii;34for(rii=SS.rbegin(); rii!=SS.rend(); ++rii)35{36cout << *rii << endl;37}3839cout << endl <<"Sample Output:"<< endl;4041cout << SS.size() << endl;42cout << SS[2] << endl;4344swap(SS[0], SS[2]);45cout << SS[2] << endl;46}
Run: ./a.out
Output:
Loop by index: The number is 10 The number is 20 The number is 30 Constant Iterator: The number is 10 The number is 20 The number is 30 Reverse Iterator: The number is 30 The number is 20 The number is 10 Sample Output: 3 The number is 30 The number is 10
Two / Three / Multi Dimensioned arrays using vector:
A two dimensional array is a vector of vectors. The vector contructor can initialize the length of the array and set the initial value.
- Example of a vector of vectors to represent a two dimensional array:
01#include <iostream>02#include <vector>0304usingnamespacestd;0506main()07{08// Declare size of two dimensional array and initialize.09vector< vector<int> > vI2Matrix(3, vector<int>(2,0));1011vI2Matrix[0][0] = 0;12vI2Matrix[0][1] = 1;13vI2Matrix[1][0] = 10;14vI2Matrix[1][1] = 11;15vI2Matrix[2][0] = 20;16vI2Matrix[2][1] = 21;1718cout <<"Loop by index:"<< endl;1920intii, jj;21for(ii=0; ii < 3; ii++)22{23for(jj=0; jj < 2; jj++)24{25cout << vI2Matrix[ii][jj] << endl;26}27}28}
Run: ./a.out
Loop by index: 0 1 10 11 20 21
A three dimensional vector would be declared as:
or declare all in one statement:01#include <iostream>02#include <vector>0304usingnamespacestd;0506main()07{08// Vector length of 3 initialized to 009vector<int> vI1Matrix(3,0);1011// Vector length of 4 initialized to hold another12// vector vI1Matrix which has been initialized to 013vector< vector<int> > vI2Matrix(4, vI1Matrix);1415// Vector of length 5 containing two dimensional vectors16vector< vector< vector<int> > > vI3Matrix(5, vI2Matrix);1718...
01#include <iostream>02#include <vector>0304usingnamespacestd;0506main()07{08vector< vector< vector<int> > > vI3Matrix(2, vector< vector<int> > (3, vector<int>(4,0)) );0910for(intkk=0; kk<4; kk++)11{12for(intjj=0; jj<3; jj++)13{14for(intii=0; ii<2; ii++)15{16cout << vI3Matrix[ii][jj][kk] << endl;17}18}19}20}
Using an iterator:
- Example of iterators used with a two dimensional vector.
01#include <iostream>02#include <vector>0304usingnamespacestd;0506main()07{08vector< vector<int> > vI2Matrix;// Declare two dimensional array09vector<int> A, B;10vector< vector<int> >::iterator iter_ii;11vector<int>::iterator iter_jj;1213A.push_back(10);14A.push_back(20);15A.push_back(30);16B.push_back(100);17B.push_back(200);18B.push_back(300);1920vI2Matrix.push_back(A);21vI2Matrix.push_back(B);2223cout << endl <<"Using Iterator:"<< endl;2425for(iter_ii=vI2Matrix.begin(); iter_ii!=vI2Matrix.end(); iter_ii++)26{27for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++)28{29cout << *iter_jj << endl;30}31}32}
Run: ./a.out
Using Iterator: 10 20 30 100 200 300
iter_jj = SS.end(); cout << *iter_jj << endl;
This will result in a "Segmentation fault" error.
No comments:
Post a Comment