Need help structuring simple resource list

Started by
2 comments, last by alvaro 10 years, 7 months ago

Hi, Im looking for a template resource list that, anyhow.

My problem is that I want the list to recognize if an item already already exist in it, and i was thinking that i might to this with a wstring name.

And rest of my program will access its resource by index in the list (and not name) thought that would be the fastest of "none pointer" solutions.

For

GenericList<Shader> shaderlist;

position = shaderlist.add(pShader, "Shader")

So I have two questions,

I need a dynamic container that can grantee the indexes of all the resource when an object is remove from the list, as I understands it std::vector does not do this, and array is not std::dynamic.

Secondly, Should I store the resource and name as a pair in that container or should I do it in two containers one resource and one for name, where the indexes are matching all the time. Not sure what will be most efficent?

thx

Advertisement

It looks like you need two containers:

* One that knows the list of names that already exist (perhaps std::unordered_set<std::wstring>, or perhaps std::unordered_map<std::wstring,int>, since you probably want to be able to recover the index from the name) and

* one to associate an integer index to an object (std::unordered_map<int,MyClass> or std::map<int,MyClass>, depending on whether preserving the order is important for anything).

It looks like you need two containers:

* One that knows the list of names that already exist (perhaps std::unordered_set<std::wstring>, or perhaps std::unordered_map<std::wstring,int>, since you probably want to be able to recover the index from the name) and

* one to associate an integer index to an object (std::unordered_map<int,MyClass> or std::map<int,MyClass>, depending on whether preserving the order is important for anything).

"Can all of them guarantee the insertion order is that what unordered means?" -- disregard this..

I see what you mean now.. But why use unordered and not plain hashmaps for instance?

std::map is some type of sorted binary tree. std::unordered_map is actually a hash. So their implementations are quite different, but the most obvious difference is that std::map keeps the elements sorted by key, while the std::unordered_map does not. Similarly for std::set and std::unordered_set.

This topic is closed to new replies.

Advertisement