You are here:

C++/vector of vectors

Advertisement


Question
Hi,
I have greated a class which has  a private member
called graph, which is a vector of vectors. I also have an inner class, declared in my public methods called edge. Now, graph is a vector full of Edge Vectors.

Declaration:
vector<vector<edge> > graph;

Suppose I have a filled edge vector called Temp;
How do I get it inro the graph, say at index 0?

I have tried graph[0].push_back(Temp).

I get an error

Can you help?  

Answer
Hi Tirra,
 Replace vector<vector<edge> > graph;  by

typedef vector <edge> newTirra;
vector <newTirra> graph;

graph.pushback(somestuff);


The following code would help you in that:
# include <iostream>
# include <iterator>
# include <vector>

using namespace std;

int main ()
{
  vector < int > vectorOfInt;
  typedef vector <int> newType;
  int x = 10;
  while (x--)
  {
     cout << "Enter an integer" << endl;
     int z ;
     cin >> z;
     vectorOfInt.push_back(z);
  }
  vector <newType> dArr;
  dArr.push_back(vectorOfInt);

/*   vector <int> ::iterator iter = vectorOfInt.begin();
  for (; iter != vectorOfInt.end(); ++iter)
     cout << *iter << endl;*/

  vector <int> ::iterator iter = dArr[0].begin();
  for (; iter != dArr[0].end(); ++iter)
     cout << *iter << endl;
  cin >> x;
  return 0;
}
Thanks,
D Rai

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.