Advertisement

vector<vector<T> > question...

Started by December 05, 2000 03:53 PM
2 comments, last by HBringer 24 years, 1 month ago
Hi folks, Alright, the books I bought on STL are more conceptual and nuts-and-bolts oriented, than acutally showing any useful code... So, can someone please show me how I''d add items to an empty vector > object?? I''m converting some routines over to vectors (from 2 dim. dynamically alloc''ed arrays); and while I know how to use push_back for a normal vector, a vector of vectors is beyond me or the scope of my reference materials. How DO I push values into the proper vectors and locations in this case? Thanks in advance! Take care, --HB
vector< vector < T > > m_vData;

to allocate a 10x10 matrix

for(int i = 0; i < 10; i++)
{
m_vData.resize(10);<br>}<br><br>to access the data like a C array (unsafe):<br><br>m_vData[5][6] = T;<br><br>I have a matrix class that uses vectors, if you want to buy the source, email me <img src="tongue.gif" width=15 height=15 align=middle><br><br> <br><br>ECKILLER<br><br>Edited by - eckiller on December 5, 2000 5:03:39 PM<br><br>Edited by - ECKILLER on December 5, 2000 5:04:55 PM
ECKILLER
Advertisement
But if I don''t use operator[] (and use iterators instead), don''t I have to have a seperate iterator for EACH vector inside the "main" vector? If not, how do I tell which "inner" vector the iterator is dealing with (or conversely, how do I move the iterator to different vectors)?

Thanks!

--HB
Here''d be a printout of a 2D vector of ints using iterators:
  void printVec (vector< vector<int> >& vec){  vector< vector<int> >::iterator outerIt;  for (outerIt = vec.begin ();       outerIt != vec.end ();       outerIt++)  {    vector<int>::iterator innerIt;    for (innerIt = outerIt->begin ();         innerIt != outerIt->end ();         innerIt++)    {      cout << *innerIt << " ";    }    cout << endl;  }}  

This topic is closed to new replies.

Advertisement