473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dereferencing a vector of vectors

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.
Dec 4 '07 #1
4 4332
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 4 '07 #2
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.
Dec 4 '07 #3
RRick
463 Expert 256MB
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.  
Dec 4 '07 #4
Oh wow! That is a really clean way to do that. Thank you very much for the solution. I was struggling!
Dec 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
9
by: Nancy Keuss | last post by:
Hi, I've created a vector of vectors of ints, and I want to pass it as a parameter to a function. Is this possible, and if so, then what is the syntax like for the function header and function...
1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.