Connecting Tech Pros Worldwide Forums | Help | Site Map

dereferencing a vector of vectors

Newbie
 
Join Date: Dec 2007
Posts: 3
#1: Dec 4 '07
I am by no means an experienced c++ programmer, but I am trying to use a vector of vectors because it is convenient to store some strings while parsing a text file. I am having trouble with the nested for loop recovery of the stored data and properly dereferencing the data. Here is a bit of test code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct s {
  8.     string strA;
  9.     string strB;
  10.     string strC;
  11. };
  12.  
  13. void main (int argc, char *argv[]) {
  14.  
  15. typedef vector <s> vstype;
  16. vstype v;
  17.  
  18.     s bugs;
  19.     bugs.strA="bees";
  20.     bugs.strB="beetles";
  21.     bugs.strC="flys";
  22.  
  23.     v.push_back(bugs);
  24.  
  25.     s bugs2;
  26.     bugs2.strA="chiggers";
  27.     bugs2.strB="moths";
  28.     bugs2.strC="praying mantii";
  29.  
  30.     v.push_back(bugs2);
  31.  
  32.     bugs.strA="mosquitos";
  33.     bugs.strB="ants";
  34.     bugs.strC="ticks";
  35.  
  36.     v.push_back(bugs);
  37.  
  38.     cout << bugs.strA << endl << endl;
  39.  
  40.     string dummy = bugs.strA;
  41.  
  42.  
  43.     for( vstype::const_iterator p=v.begin(); p!=v.end(); ++p) {
  44.          cout << "3 BUGS" << endl << (*p).strA << endl << (*p).strB << endl << (*p).strC << endl << endl;
  45.     }
  46.  
  47.     v.clear();
  48.  
  49.     for( p=v.begin(); p!=v.end(); ++p) {
  50.          cout << "3 BUGS" << endl << (*p).strA << endl << (*p).strB << endl << (*p).strC << endl << endl;
  51.     }
  52.  
  53.     cout << "===============" << endl << endl;
  54.  
  55. vector <vstype> vx;
  56.  
  57.     vx.push_back(v);
  58.  
  59.     v[0].strA="aphids";
  60.     v[0].strB="stink bugs";
  61.     v[0].strC="dragonflys";
  62.  
  63.     vx.push_back(v);
  64.  
  65.     // 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?
  66.     // I am assuming that you do not need to use the vector[x][y] notation.
  67.  
  68.     for (vector<vstype>::const_iterator s=vx.begin(); s!=vx.end(); s++) {
  69.         for (p=(*s).begin(); p!=(*s).end(); p++) {
  70.             cout << ????? << endl;
  71.         }
  72.         cout << endl;
  73.     }
  74. }
I suppose I could try literal indexing such as vvec[x][y], but was curious whether this method would work.

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#2: Dec 4 '07

re: dereferencing a vector of vectors


Line 47 has v.clear() and I think that deletes all of the elements in your vector.

Exactly what are you trying to do??

If you need to store strings, then use vector<string>.

You say you are using vector of vectors but I don't see that. It looks just like a vector<s> where is a struct that contains three strings.

A vector of vectors would be:

Expand|Select|Wrap|Line Numbers
  1. vector<vector<string> > v;
  2.  
an din this case a v[i][j] will work to identify a string - provided the i and j are valid indexes into the vectors. That is, that they and do not have values that are outside the size() of the vectors.
Newbie
 
Join Date: Dec 2007
Posts: 3
#3: Dec 4 '07

re: dereferencing a vector of vectors


Thank you for your reply.

I found the vector.clear line after I posted - a mistaken leftover from a prior debug. I apologize. Please disregard this line.

Line 15 is a typedef for the vector (and vector of vectors which is declared on line 55).

The for loop on lines 68-74 is what I am interested in and more specifically, how to de-reference it effectively.

While researching this on the web, I found two methods to execute this for loop to reach the members of vectors in nested for loops.

The first is the one I listed. The example of this method I found was for a single vector - not a vector of vectors. I took the liberty of trying to expand it. I think this is using pointers (the iterator) to the actual elements of the vector. The pointer is indexed from a start (p=v.begin(); ) to the last element (p=v.end(); ) and the elements are accessed with the syntax ((*p).strA if it were a single vector for loop.)

This is effective for a single vector, but I am unsure how to implement it with a vector of vectors and reference the appropriate start, end, and each element.

A second method of doing this I found used an ordinary int as indexes in the for loop as follows:

Expand|Select|Wrap|Line Numbers
  1.     for (int a=0; a<vx.size(); a++) {
  2.         cout << "a = " << a << endl;
  3.         for (int b=0; b<vx.at(a).size(); b++) {
  4.             cout << "b = " << b << endl;
  5.             cout << vx[a][b].strA << endl;
  6.             cout << vx[a][b].strB << endl;
  7.             cout << vx[a][b].strC << endl << endl;
  8.         }
  9.         cout << endl;
  10.     }
This works, but I still wonder how to get the original single for loop to work correctly as a nested for loop for a vector of vectors.
RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#4: Dec 4 '07

re: dereferencing a vector of vectors


You've got the right idea for accessing the vectors inside another vector by using an iterator. Your p variable is now a vector and you can get an iterator to p to access the struct s. Once you have struct s, you can access its variables.

Take a look at the following code. I changed the names a bit for my sanity and ease. You can access the iterator value by * or ->, your choice.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct S {
  7.     string strA;
  8.     string strB;
  9.     string strC;
  10. };
  11.  
  12. typedef vector <S> Vs;
  13. typedef vector <Vs> Vvs;
  14.  
  15.  
  16. int main (void) 
  17. {
  18.     S ss;  ss.strA="AAA";  ss.strB="BBB";  ss.strC="CCC"; 
  19.     Vs  vs;  vs.push_back( ss); vs.push_back( ss); 
  20.     Vvs vx;  vx.push_back( vs);
  21.  
  22.     for ( Vvs::const_iterator ivv=vx.begin(); ivv!=vx.end();  ivv++)
  23.     {
  24.         for ( Vs::const_iterator iv=ivv->begin(); iv!=ivv->end(); iv++)
  25.         {
  26.             cout << "A: " << iv->strA << endl;
  27.             cout << "B: " << iv->strB << endl;
  28.             cout << "C: " << iv->strC << endl << endl;
  29.         }
  30.     }
  31.  
  32.     return 0;
  33. }
  34.  
Newbie
 
Join Date: Dec 2007
Posts: 3
#5: Dec 6 '07

re: dereferencing a vector of vectors


Oh wow! That is a really clean way to do that. Thank you very much for the solution. I was struggling!
Reply