Prev: Dynamic Allocation of Structure Array
Next: Can auto_ptr<> be used with void and malloc()/free()
From: Jack on 1 May 2010 04:44 Dear all, std::vector<x> finalvector; std::vector<x> x1; std::vector<std::vector<x>> x2; for (...) { for (...) { x1.push_back(data); } x2.push_back(x1); } std::vector<std::vector<x>>::reverse_iterator it; for (it = x2.rbegin(); it != x2.end(); it++) { finalvector.push_back(**it); <<<<<<<<<<<<< compilation error } I'd like to reverse a grid in a y-axis while keeping the x-axis components intact. For example 1 2 3 4 5 6 7 8 a b c d e f g h to a b c d e f g h 1 2 3 4 5 6 7 8 Is it the correct way of doing it? Thanks in advance Jack
From: Jack on 1 May 2010 04:58 > for (it = x2.rbegin(); it != x2.end(); it++) Sorry, typo x2.rend(), still doesn't compile Thanks
From: David Lowndes on 1 May 2010 08:40 >std::vector<x> finalvector; finalvector should be the same type as x2, i.e.: std::vector<std::vector<x>> finalvector; and then use: finalvector.push_back( *it ); However, an easier solution is to use: #include <algorithm> std::reverse( x2.begin(), x2.end() ); Dave
From: Jack on 1 May 2010 08:49 Thanks David, I'll give it a try! Jack
From: Stephan T. Lavavej [MSFT] on 1 May 2010 23:33 You should point out that std::reverse() will be significantly more efficient. It swaps instead of copying. In general, it's a good idea to use Standard Library algorithms whenever possible. Their designers and implementers worked hard to make them as efficient as possible. Using Standard Library containers is simply the first step. Stephan T. Lavavej Visual C++ Libraries Developer "David Lowndes" <DavidL(a)example.invalid> wrote in message news:l68ot5dlo896r90nrmj0jejgnoiku68ek7(a)4ax.com... > >std::vector<x> finalvector; > > finalvector should be the same type as x2, i.e.: > > std::vector<std::vector<x>> finalvector; > > and then use: > > finalvector.push_back( *it ); > > However, an easier solution is to use: > > #include <algorithm> > > std::reverse( x2.begin(), x2.end() ); > > Dave
|
Pages: 1 Prev: Dynamic Allocation of Structure Array Next: Can auto_ptr<> be used with void and malloc()/free() |