Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

Arrays in Templates

This topic is 4899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to have an array or a linked-list effect in a template. I don't really want to use the predefined "list" template of the STL, don't really know how it works... so i want to create my own. How would i be able to have an array, BUT i don't want to have to define the size at the start, everytime another array is added, i want the size of the array to increase.

Share this post


Link to post
Share on other sites
Advertisement
There are some options:

1) Use a dynamic array, resize it every addition and move all the data to the new location(Very slow)

2) Allocate more than needed in say, additional chunks of 16. Keep adding to the back of the memory and keep track of the size. Resize after the limit is reached by another 16 and copy. A bit faster, but still slow for alot of additions/removal.

3) Write your own custom Linked List implementation and store nodes to the data. Alot faster, but doesn't allow random access like an array.

4) Learn to use std::list

These four are your best options. I suggest you learn how to write your own linked list and then learn to use the STL. Using the STL is quite simple, esp. list.

Example:

// Create a list and add new items:
std::list<int> IntList; // Stores integers
IntList.push_back(10); // Add a new item
IntList.push_back(10); // Another one
IntList.push_back(12); // And another one
size_t size = IntList.size(); // Contains 3

// Walk the list and display it's contents
for (std::list<int>::iterator it = IntList.begin(); it != IntList.end(); ++it)
{
int i = *it; // Using *it will "reveal" the data stored in it
cout << i << endl;

// Let's delete the item
it = IntList.erase(it); // See the assignment to it. Not doing so, will make
// the iterator invalid and cause your app it crash.
}

// We're done, clear all items
IntList.clear();


Toolmaker

Share this post


Link to post
Share on other sites
Arrays are the spawn of the devil.

The problem you describe is solved by every single modern container class [such as STL's list]. Use the right tools for the job. The STL is part of the language. You'll not regret learning it.

Share this post


Link to post
Share on other sites
ok, thank you for that, can someone point me to a tutorial or something so i CAN learn it? just looking at the code is too confusing... everything is all over the place

Share this post


Link to post
Share on other sites
std::list is simple enough that you should be able to pick it up from the MSDN docs. Check out the list members to see what you can do with std::list, examples are provided with most of the member functions. For now don't worry too much about the Allocator template, leaving it out will assume the default which is fine for most purposes.

Share this post


Link to post
Share on other sites
Actually, now i think i'm going to use the std::map template, someone told me about this and i think it would be better so that way i can just refer to each texture by it's name AND being able to be stored in a template.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!