Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Trouble with .push_back()

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:

(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:
(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
   whitepixellist.clear();
and
   cout << whitepixellist.size();
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Trouble with .push_back()

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?
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: Trouble with .push_back()

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: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Trouble with .push_back()

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:
//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
}
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On