I have learned a lot about templates and library programming from reading and using the Boost code. Recently I have been finding myself doing something that I would call Static Maps. Basically I would like to use an array of structs as if it were a map or associative array. Does boost have anything like this or can anyone recommend something like that? Here is a simple example: struct entry { const char* key; const char* value; }; const entry entries[] = { { "one", "Hello" }, { "two", "World" }, { "three", "!" } }; { some_type the_map = make_static_map( entries ); const char* word = the_map[ "one" ]; } This is a real poor example, but shows the idea. The struct could be any types that are less-than comparable; maybe there is some special arrangment of key and type. But the idea is there. I would like to be able to lookup items in a static array of items using array notation (other than numerically indexed). For extra credit, the interface would be writeable also; aka a fixed array space map. Any idea? Comments? Suggestions? ..Duane
Maybe I missed something, but doesn't an std::map
--- At Mon, 14 Jan 2002 14:29:39 -0800, Mike Thompson wrote:
Maybe I missed something, but doesn't an std::map
do this??
Yes, sort of. But std::map<> is big, heavy, slow and dynamic. This is totally static data that doesnt change for the lifetime of the application. A simple lookup table. The table requires no dynamic memory allocations. Really what this template class would do would be to hide the searching of the table as well as provide a single source of the search code.
Duane Murphy
wrote: I have learned a lot about templates and library programming from reading and using the Boost code. Recently I have been finding myself doing something that I would call Static Maps. Basically I would like to use an array of structs as if it were a map or associative array. Does boost have anything like this or can anyone recommend something like that?
Here is a simple example:
struct entry { const char* key; const char* value; };
const entry entries[] = { { "one", "Hello" }, { "two", "World" }, { "three", "!" } };
{ some_type the_map = make_static_map( entries ); const char* word = the_map[ "one" ];
}
This is a real poor example, but shows the idea. The struct could be any types that are less-than comparable; maybe there is some special arrangment of key and type. But the idea is there. I would like to be able to lookup items in a static array of items using array notation (other than numerically indexed).
For extra credit, the interface would be writeable also; aka a fixed array space map.
Any idea? Comments? Suggestions?
..Duane
participants (2)
-
Duane Murphy
-
Mike Thompson