Trouble with .push_back()

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Trouble with .push_back()

Postposted on Sun Oct 30, 2011 12:01 pm

Why is it that here:
viewtopic.php?f=20&t=78340
I can use openlist[0].push_back(), but in this following code:

Code: Select all
(in the .h)
   list<int> whitepixellist;
(in the .cpp)
whitepixellist.resize(2);
   whitepixellist[0].push_back(whereyouatnow[0]);
   whitepixellist[1].push_back(whereyouatnow[1]);


it always gives me error C2109 and C2228 (Visual Studio 2003). How else is a person supposed to insert values into the second dimension of a list?

Also when I do:
Code: Select all
(in the .h)
   list<int> whitepixellist(2);
(in the .cpp)
   whitepixellist[0].push_back(whereyouatnow[0]);
   whitepixellist[1].push_back(whereyouatnow[1]);

I get more C2228 errors from lines such as
Code: Select all
   whitepixellist.clear();
and
   cout << whitepixellist.size();
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: Trouble with .push_back()

Postposted on Sun Oct 30, 2011 12:15 pm

std::list is not a vector, there is no operator[] that you can use like an array. Therefore, whitepixellist[0] fails with C2109. And since whitepixellist[0] is an invalid expression, the compiler has trouble understanding .push_back() so you get C2228.

Putting whitepixellist(2) is just pre-allocating the list to a size of 2, you want whitepixellist[2] if you want an array of 2 lists.

Don't know about your last case, but I suspect you were also trying to use whitepixellist[0].clear() or something to that effect, so you are continuing to get C2228's.

Looks like you are still new with the operators (() vs []), and STL containers in general. Need more reading up and practice young grasshopper. :D And btw, if you can, switch to at least VS2008, much better compiler and debugger messages for STL stuff, with more compliance to standard C++.

Again, have to ask, what are you trying to do exactly? Do you need list or vector? Do you understand the characteristics and the price to pay for choosing each one?
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24440
Joined: Mon May 24, 2004 2:19 am

Re: Trouble with .push_back()

Postposted on Sun Oct 30, 2011 12:57 pm

Thanks. I want to build a list of white pixels found in an image (representing a border) and around 60 times a second determine if a human is nearing one of them too quickly and is going to overstep the border. So every element is going to be accessed repeatedly, and I was using STL containers because I was new to them and wanted an excuse to use them.

deque, list, vector, I don't know the performance tradeoffs for using them, but for the purposes of this simple algorithm I believe they're functionally interchangeable.

The project itself wasn't written by me - it's old, and the last time I tried compiling it with VS2008 it came up with so many errors, mostly from the old libraries. It's just not worth the time.
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: Trouble with .push_back()

Postposted on Thu Nov 03, 2011 1:45 am

Do those white pixels stay the same once the image is processed initially? If that's the case you are looking at array-like characteristics and vector may seem to be appropriate. List is going to be a bit slower if you walk the list 60 times a second (but have to really show in a profiler to be sure of course).

So I take it that you are trying to create 2 lists to store the x and y coordinates? Since you did not mention how much raw performance you need and that you want an excuse to use STL stuff, then I suggest using a vector of pairs. So it would look like the following:
Code: Select all
//std::vector<std::pair<int, int> > whitePixels;
// or for better readability using typedefs
typedef std::pair<int, int> Pixel;
typedef std::vector<Pixel> PixelList;
PixelList whitePixels;

// usage - .first = x, .second = y
// push item
whitePixels.push_back(std::make_pair(someX, someY));
// reference
for (PixelList::const_iterator i = whitePixels.begin(); i != whitePixels.end(); ++i) {
    // ..
    whiteX = i->first;
    whiteY = i->second;
   // do something with whiteX and whiteY, or you can just deref the Pixel type
}
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24440
Joined: Mon May 24, 2004 2:19 am


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests