Jump to content
  • Advertisement
Sign in to follow this  
GameMasterXL

Custom dynamic arrays?

This topic is 4842 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 am wondering how would i go about creating my own dynamic array that could increase and decrease at run-time? i am using C++. This will then be used for a array that can store stuff like this Row 1 Msg Row 2 Hello everyone how are you all. So it can store very long sentances and be able to store rows of data so i think i would need a 2-dimensional array for this? and for dynamicness i would need to use New? and then dealocate the memory using delete.

Share this post


Link to post
Share on other sites
Advertisement
I heard though that vectors<> are slow is this true and the type of array i am trying to build will store the data like above but then i want keys for it storing integral values like 1, Hello how are you today. ect. But the dynamic array stuff is the BIG one. If i used the STL for the above example i would need to use std::map<> and i don't know how fast they are.

Share this post


Link to post
Share on other sites
No, vectors arnt slow, well certainly not slower than you could write one. They are used for everything.

If you are looking to store abitrary data by a key value then a map is what you want. The look up time for a map is extrememly quick with an unsorted collection.

ace

Share this post


Link to post
Share on other sites
If I understand you correctly you want to store your data like this:

1 -> "Hello"
15 -> "Wassup"
28 -> "It's me, Mario!"

If so, then std::map<int, std::string> is probably ok. And the STL containers are already performance optimized, so I'd be surprised if you could easily write a faster solution.

Ouch, rating hit??[disturbed]

Share this post


Link to post
Share on other sites
Thanks everyone for all your replies :). I realy apriciate it, this is for my parser for storing tokense and there equivelant token types lol.

Share this post


Link to post
Share on other sites
vector<> can be as fast as a plain C array, but it is simply an array of elements
with some more convenience. Searching would mean traversing the array, in worst case up to the last element, which is genereally slower than a lookup in a map.

Share this post


Link to post
Share on other sites
Quote:
Original post by GameMasterXL
How would i go about getting each key and value pair from the map<> starting from the front and reading each pair in?



map<Key, Value> table;
map<Key, Value>::iterator itor;

for(itor=table.begin(), itor!=table.end(); ++itor)
{
Key k = itor->first;
Value v = itor->second;
}

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!