Skip to main content
GameDev.net gamedev.net
🔒 Locked

Stl Problems

Started by Brad8383 Jan 24, 2003 at 12:21 PM 5 replies 400+ views
Original Post
Brad8383
Brad8383
Im storing my tiles like this: vector< vector < DxTile > > Map; I fill that in my app and send it by value to the map engine, and everything works fine. Except when I send it as a pointer im having problems. Inside the map engine where before I did map.size i do map->size. no problems there. When I do map[0].size that stays the same. The big problem is when i want to do map[0][0].render. With the dot its wanting to call the stl functions and if I do map[0][0]->render I get an error that '->' isnt a member of stl:blah blah. Anyone good with stl or pointers see what I'm doing wrong here? [edited by - Brad8383 on January 24, 2003 1:24:51 PM]
daerid
daerid
if you declare your class like this: vector< vector < DxTile > > Map;

and your function looks like this:

void MapEngine(vector< vector < DxTile > >* pTiles);

then inside that function you have to access the members like this:

(*pTiles)[0][0].render();
OR
pTiles->at(0)[0].render();


[edited by - daerid on January 24, 2003 1:30:13 PM]
daerid@gmail.com
sbennett
sbennett
To check the situation:

void foo(vector< vector > *map)
{
...
map[0][0].render();
...
}

In this case, map[0] returns *map, IE the vector of vectors.
map[0][0] returns the first vector of tiles in this vector; render() is not a member of std::vector.

What you need is:

(*map)[0][0].render();

Or, better still, use iterators, as they are more efficient than using vector::at() or [] operators in a loop.
daerid
daerid
quote:
Original post by sbennett
Or, better still, use iterators, as they are more efficient than using vector::at() or [] operators in a loop.


If your using a decently optimizing compiler, it should produce equivalent (for all intents and purposes) code for either iterators or at()/[]. The only difference would be an additional index variable for using at()/[], but that wouldn't affect speed.

[edited by - daerid on January 24, 2003 1:54:21 PM]
daerid@gmail.com
Shannon Barber
Shannon Barber

  
pMap[0][i][j].render();
//pMap[0] is equivalent to (*pMap)



You could also pass the map by reference, or constant reference.


quote:
daerid
If your using a decently optimizing compiler, it should produce identical code for either iterators or at()/[]. The only difference would be an additional index variable for using at()/[], but that wouldn't affect speed.


You say the code's identical, and then you state how it's different! This is a convoluted test case though, because vector[vector] is an inefficent layout. Ideally, you'd want one continuous piece of memory for the whole map, and have it in memory the way you want to transverse it (i.e either row or column major). It probably won't matter much though, any which way will be fast enough.

There's a multi-array container in boost.

- Magmai Kai Holmlor

"Oh, like you've never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]



[edited by - Magmai Kai Holmlor on January 24, 2003 6:38:21 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
daerid
daerid
Good call. I edited my previous post.
daerid@gmail.com
cMADsc
cMADsc
Let me see if I understand the question correctly. first, is DxTile a class and is "render" a member function of DxTile?
if you have a sequence of pointers to objects, u can call
member functions for them:

void ptrfoo(const std::vector < MyClass * > &coll)
{
using std::for_each;
using std::bin2nd;
using std::mem_fun;

for_each(coll.begin(), coll.end(), mem_fun(&MyClass::myfunction));
}



//or to render all in a single statement

for_each(Map.begin(), Map.end(), mem_fun(DxTile::render));


I hope this is what u are looking for!



-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."



[edited by - cMADsc on January 24, 2003 6:19:08 PM]
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.