Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » std::vector, Where Have You Been All My Life?

This thread is locked; no one can reply to it. rss feed Print
 1   2 
std::vector, Where Have You Been All My Life?
Kris Asick
Member #1,424
July 2001

I've known of and have been using the std::vector class for about a dozen weeks now, primarily while making my graphics application, and I'm starting to wonder how I ever programmed without it!

For starters, making the file handler for my graphics program has been absolute Hell... specifically because of all the nuances in what the thing should do when you click on certain things, or type in certain file names, or switch your desired file format, etc. etc. But the process of actually creating the list of files from the current working directory was extremely simple, thanks to _stricmp() for sorting and the findfirst() and findnext() functions to get the filenames, but most importantly, the std::vector object that holds the list of filenames and file attributes without having to do messy memory allocation or whatnot.

I've also got std::vector objects holding the brush history, undo history, and bitmap pages.

I've been slowly losing my desire to work with static arrays, which, 12 weeks ago, I was convinced would never happen. As a result, I've been going through the docs for my game engine and the first game I'm going to make with it and altering many of their static-allocated features to be dynamic. (Some of the static-ness is still there, but only where it makes sense to have small, predefined limits.)

The new and delete commands make perfect sense to me now as well, and I haven't touched malloc() or calloc() since PixelShips Retro, though it took over half an hour for me to fix a bug in my graphics program that involved nothing more than changing "BITMAP *bitmap" in a function definition to a "BITMAP *&bitmap"...

...so I guess stupid mistakes are still going to happen... ::)

Anyways, I'm fully confident in using dynamic arrays and std::vector objects now, and it's hard to imagine that I used to program without them not long ago...

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Billybob
Member #3,136
January 2003

Amen brother.

Goalie Ca
Member #2,579
July 2002
avatar

Don't stop at std::vector. Take a look at STL. STL offers a number of simple algorithms. std::copy, transform, find_max, etc. all make life much simpler.

And then when you're really serious take a look at boost. Boost is doing such a good job that most of the C++0x library changes (ie: the next c++ standard) is coming from the boost libraries. In fact, its the same way that the STL (including std::vector) managed to work its way into C++ everywhere.

-------------
Bah weep granah weep nini bong!

Mark Oates
Member #1,146
March 2001
avatar

You bring back memories of when I first discovered the vector. 90% of all the programming I uses that in one way or another. :)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Kibiz0r
Member #6,203
September 2005
avatar

std::map is also quite handy in certain cases.

CGamesPlay
Member #2,559
July 2002
avatar

I think I use std::list the most.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Billybob
Member #3,136
January 2003

Oh, and don't forget std::string. It isn't the best, by far, but with a handy to_string template function life is a breeze. 8-)

template<typename T>
std::string to_string(const T &value)
{
  std::ostringstream ret;
  ret << value;
  return ret.str();
}

HoHo
Member #4,534
April 2004
avatar

std::vector is pretty much the only container I ever use. I remember using std::map for a couple of times to store some settings but that's it. It is not that I don't need know else, it's just that all my programs work perfectly fine with vectors :)

Of cource there are a lot more of those interesting headers. Algorithm and numeric come to my mind first. I suggest to at least read the TOC of SGI STL documentation to get some idea what functionality does STL have.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Kikaru
Member #7,616
August 2006
avatar

I very often use STD vector, though occasionally use an array for some things that are small, delicate, and have a defined max size.

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

Don't stop at std::vector. Take a look at STL. STL offers a number of simple algorithms. std::copy, transform, find_max, etc. all make life much simpler.

Bookmark this link.

Simple example:

// Before
for(vector<string>::iterator itr = strings.begin(); itr != strings.end(); ++itr)
  itr->foobar();

// After
for_each(strings.begin(), strings.end(), mem_fun_ref(&string::foobar));

Billybob
Member #3,136
January 2003

// After after
BOOST_FOREACH(string item, strings)
     item.foobar();

8-)

Goalie Ca
Member #2,579
July 2002
avatar

well billy bob, seeing as how you use boost and you gave a stringstream example you should probably like this:

void log_errno(int yoko)
{
    log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
}

If you read the hpp file, you'll notice it uses a stringstream (sstream or the boost implementation based on #defines).

Also, the idea of using for_each and transform has got me thinking about functional programming. That's where i discovered boost lambda (which will be in C++0x).

for_each(a.begin(), a.end(), 
         if_(_1 % 2 == 0)[ cout << _1 ])

Being C++ it isn't half as elegant as python list comprehensions but its pretty good for a start.

-------------
Bah weep granah weep nini bong!

Billybob
Member #3,136
January 2003

Quote:

well billy bob, seeing as how you use boost and you gave a stringstream example you should probably like this:

Niiice. I don't actually use boost, just the BOOST_FOREACH header. But that's a neat trick anyway. :)

axilmar
Member #1,204
April 2001

Kris Asick said:

Anyways, I'm fully confident in using dynamic arrays and std::vector objects now, and it's hard to imagine that I used to program without them not long ago...

The path of illumination has opened before you. At the end of the corridor lies a door which says "LISP", which, when you reach it, you can claim reaching the ultimate state of enlightenment as a programmer.

ImLeftFooted
Member #3,935
October 2003
avatar

// After after
BOOST_FOREACH(string item, strings)
     item.foobar();

Now make your code go over some arbitrary memory, or how about even a subset of the strings list...

Right. And thats why I use the STL. Boost is mostly crap anyway. This sort of method: boost::lexical_cast should not be using stringstream anyway, thats tons of unnecessary overhead. There are more efficient ways to do that.

Boost is generally lame anyway. Its key "popular" features are just thin wrappers around C++ features.

And come on, macros? I thought we were done with those.

Quote:

for_each(a.begin(), a.end(), 
         if_(_1 % 2 == 0)[ cout << _1 ])

template<int power, typename Parm>
bool powerOf(Parm p) { return p % power == 0; }

^^ put that in a header somewhere, very reusable.
Now to do ugly lambada crap in a clean way:

copy_if(a.begin(), a.end(), ostream_iterator<int>(cout), PowerOf<2>);

piccolo
Member #3,163
January 2003
avatar

woa i have so much to learn. Is this used in the work place? example a database programing position.

wow
-------------------------------
i am who you are not am i

Hard Rock
Member #1,547
September 2001
avatar

Most of whats is being shown here are just neat tricks that help you stand out from being a good programmer to being a better programmer.

Now regarding the work place it really depends. For a database programming job, chances are you wont need to know anything here. Very rarely is database code done in C++, and if it is, it's going to be abstracted away some how.

.Net, Java etc are languages that you'll find most database jobs use and they wont need any of these clever hacks. Mostly because stuff like this is built into the language, or added in someway.(For each loops are the best example).

Now if you're developing a C++ program in your job, then it could be. However you want to avoid being too fancy with this kind of stuff since it might work nice, but some other people might not understand the code etc. But in the end its not necessary and clean and easy to understand and efficient is more important then using neat tricks to simplify things. In fact depending on the requirements of the project, you might not even be able to do some stuff you would like.

Now if your asking if you need to the STL to code C++ apps, and you better be familiar with STL and not just learning it for the first time when you walk in.

_________________________________________________
Hard Rock
[ Stars Dev Company ][ Twitter ][Global Warming: ARA My TINS 07 Entry][Pong Ultra Website][GifAllegS Ver 1.07]
"Well there's also coolwebsearch but we'll let that be an IE exclusive feature" - arielb on the New Browser Plugins "What's better, HTML or Variables?"

Onewing
Member #6,152
August 2005
avatar

Quote:

std::vector, Where Have You Been All My Life?

I just said this today, only it was "PHP" instead of "std::vector".

------------
Solo-Games.org | My Tech Blog: The Digital Helm

tobing
Member #5,213
November 2004
avatar

You really want to use different STL containers for different purposes. Look into deque, set, map and list as well, not only vector, and get familiar with them, so you can pick the right thing for the task at hand.

Goalie Ca
Member #2,579
July 2002
avatar

Quote:

And come on, macros? I thought we were done with those.

Agreed. The way its implemented is ugly. Lambda functions are really nice though. Your solution is nice but the problem is that you still have to declare something outside the function.

The thing that really irks me about c++ is that you cannot define a function inside of a function. SO you have to make a private member somewhere...

A lot of what i do is program math for applied problems like dt-mri imaging. C++ is sort of the general purpose does all language for me but i'd still love to see more features of a functional language. There are many things that functional languages simply cannot do easily or cleanly enough.

-------------
Bah weep granah weep nini bong!

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

Your solution is nice but the problem is that you still have to declare something outside the function.

Yeah, but at least its reusable.

if(powerOf<5>(25))
  cout << "25 is a power of 5!\n";

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

I just said this today, only it was "PHP" instead of "std::vector".

OMG, I had the exact same thing just a week ago.
And again today, while diving into the PHP image functions. Woooo-hoo! It's almost like a full allegro built into the language! Well, the graphics part at least...

Quote:

Agreed. The way its implemented is ugly. Lambda functions are really nice though. Your solution is nice but the problem is that you still have to declare something outside the function.

Yeah. I'd love something like this:

vector<string> mystrlst;

foreach(mystrlst, lambda(string& s) { cout << s; } );

Generally, lambda functions could be soooo useful; for example, to pass as a callback...
Or php-like:

vector<string> mystrlst;

foreach(mystrlst as s) {
  cout << s;
}

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Carrus85
Member #2,633
August 2002
avatar

Tobais, that already exists:

vector<string> mystrlist;
std::copy(mystrlist.begin(), mystrlist.end(), ostream_iterator<string>(cout, "item seperator"));

Tobias Dammers
Member #2,604
August 2002
avatar

The cout is just an example; my point is that where the cout statement sits, there could be arbitrary code; anything you like.
Something like the lambda thing is possible already, with the minor drawback that you cannot define the function on-the-fly, it must be a function or member function somewhere.
And templates make up for a lot, often allowing for more elegant solutions.

But I do sometimes miss a more convenient iterating mechanism in C++.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Taiko Keiji
Member #8,307
February 2007
avatar

WOW, this article got me looking into vectors, I had heard about them before but never understood how to use them, now I'm in the same boat as you and don't know how I got by without them!

I WUB WOOOOUU!!
sorry, read vgcats on vgcats.com to understand.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

 1   2 


Go to: