Topic: STL using Vectors


Author: "Robert Youdan" <rob@event-horizonsXXNOSPAMXX.com.au>
Date: 2000/02/19
Raw View
I am not commenting on the order of your rows, as that is up to you, but you
should initialise your iterator with a real position in your vector:

A.push_back(row1);
vector<int>::iterator t = A.begin();
A.insert(t, row2);
....
....

Rob



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/02/19
Raw View
In article <lMSq4.818$jT5.82315@newsin1.ispchannel.com>, Tony
Colavecchio <colavecchio@lawton.ispchannel.com> writes
>I am trying to make a vector of vectors to form a matrix.
>My big problem is to insert items into the vector.  If I use push_back then
>my rows of the matrix are out of order.

??? How so?  I would have thought that it got them in exactly the
correct order -- last one last.
> I want to use insert, but keep
>getting errors on my iterator.  Here is a little of what I have.
>
>vector<int> row1;
>vector<int> row2;
>vector<int> row3;
>vector< vector<int> > A;
>
>vector<int>::iterator t = 0

But what is t for, it should be an iterator for A and it isn't of the
right type. Worse, why are you initialising it to 0? It needs to point
to where you want to insert (A.begin() or A.end() seem good candidates,
it would also be a good idea to capture the return value and be careful
of invalidated iterators.

>A.insert(t,row1);

Obviously, if you think about it, this will not work as t is the wrong
type of iterator, wrongly initilaised.

>t++;
>A.insert(t,row2);
>t++;
>A.insert(t,row3);
>
>It is giving me all sorts of crazy errors.  Not sure whats going on.  I am
>understanding pretty well how the vectors are working all except for the
>iterator.  I would appreciate any info, or if you know of a more efficient
>way to
>go about building a matrix.  Thanks in advance.

Well there are a large number of matrix libraries available for
download, why not look at one of them.

>
>AC
>
>
>
>
>
>
>      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
>      [ about comp.lang.c++.moderated. First time posters: do this! ]
>
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Xenos <nobody@nowhere.com>
Date: 2000/02/24
Raw View
For one thing, don't initialize an iterator with an integer, use the
begin() method.  For another, vector iterators become invalid after
the vector is modified.  I don't know what you are trying to do, but
you should look into using the map or multimap containers.

On 18 Feb 00 07:49:42 GMT, "Tony Colavecchio"
<colavecchio@lawton.ispchannel.com> wrote:

>I am trying to make a vector of vectors to form a matrix.
>My big problem is to insert items into the vector.  If I use push_back then
>my rows of the matrix are out of order.  I want to use insert, but keep
>getting errors on my iterator.  Here is a little of what I have.
>
>vector<int> row1;
>vector<int> row2;
>vector<int> row3;
>vector< vector<int> > A;
>
>vector<int>::iterator t = 0
>A.insert(t,row1);
>t++;
>A.insert(t,row2);
>t++;
>A.insert(t,row3);
>
>It is giving me all sorts of crazy errors.  Not sure whats going on.  I am
>understanding pretty well how the vectors are working all except for the
>iterator.  I would appreciate any info, or if you know of a more efficient
>way to
>go about building a matrix.  Thanks in advance.
>
>AC
>
>
>
>
>
>
>      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
>      [ about comp.lang.c++.moderated. First time posters: do this! ]
>
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]