Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- struct s {
- string strA;
- string strB;
- string strC;
- };
- void main (int argc, char *argv[]) {
- typedef vector <s> vstype;
- vstype v;
- s bugs;
- bugs.strA="bees";
- bugs.strB="beetles";
- bugs.strC="flys";
- v.push_back(bugs);
- s bugs2;
- bugs2.strA="chiggers";
- bugs2.strB="moths";
- bugs2.strC="praying mantii";
- v.push_back(bugs2);
- bugs.strA="mosquitos";
- bugs.strB="ants";
- bugs.strC="ticks";
- v.push_back(bugs);
- cout << bugs.strA << endl << endl;
- string dummy = bugs.strA;
- for( vstype::const_iterator p=v.begin(); p!=v.end(); ++p) {
- cout << "3 BUGS" << endl << (*p).strA << endl << (*p).strB << endl << (*p).strC << endl << endl;
- }
- v.clear();
- for( p=v.begin(); p!=v.end(); ++p) {
- cout << "3 BUGS" << endl << (*p).strA << endl << (*p).strB << endl << (*p).strC << endl << endl;
- }
- cout << "===============" << endl << endl;
- vector <vstype> vx;
- vx.push_back(v);
- v[0].strA="aphids";
- v[0].strB="stink bugs";
- v[0].strC="dragonflys";
- vx.push_back(v);
- // How do you properly dereference the individual items in the following nested for loop to print all values of strings in the vector of vectors?
- // I am assuming that you do not need to use the vector[x][y] notation.
- for (vector<vstype>::const_iterator s=vx.begin(); s!=vx.end(); s++) {
- for (p=(*s).begin(); p!=(*s).end(); p++) {
- cout << ????? << endl;
- }
- cout << endl;
- }
- }