Original Post
Greetings, I have a problem that's been bugging me for quite a while. I'm working on a small game project that uses Lua as a scripting language to interface with the engine itself. Anyway, I have a function written in lua to access some "CVar's" I've setup, such as the resolution, bit depth, etc, like so: GetCVar("screenResolution"); -- this calls a C function, that accesses the map and gets the value, then sends it back to the lua script. As I was saying, I can't quite figure out how to make std::map work like I want it to. Maybe I just can't use it like this, but I'd like to know for sure. This is a small example I've written to illistrate my problem: Toward the end you can see the problem, I can't use anything other than directly quoted strings as a key. Is there anything I can do to get passed this? Thanks in advance. Edit: To clerify; I do not intend on using std::string for the final code, that was just used to quicker illustrate the problem, and I intend on using a hash map eventially, as I hear they're faster in terms of lookup, etc.
#include <iostream>
#include <map>
// value and length, not used in full yet
typedef struct _val {
char value[128];
int length;
} val;
// test class to hold map/iterator I use
class cmap {
public:
std::map<const char *, val> m_Values;
std::map<const char *, val>::iterator m_it;
};
// class
cmap maps;
int main(int argc, char *argv[]) {
// copy some names
val vals1, vals2;
strcpy(vals1.value, "Flimflam");
strcpy(vals2.value, "Deathwing");
// add these to the map
maps.m_Values["characterName"] = vals1;
maps.m_Values["realmName"] = vals2;
// begin loop to query data
std::string str;
while(true) {
printf("Name: ");
std::cin >> str;
if(str == "q") break;
// uncomment to directly reference realmName. this WORKS
// maps.m_it = maps.m_Values.find("realmName");
// use the input as the value to find. Does NOT work
maps.m_it = maps.m_Values.find(str.c_str());
// print results to the query
printf(" Results: %s\n\n", (*maps.m_it).second.value);
}
return 0;
}