Sign in to follow this  

Creating IDs that use strings instead of ints? (C++)

This topic is 3462 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

Ok, I have this program that will create "variables" that will have a name requested by the user and have it store values to it. I did try using map header, but I don't think I am using it properly since the id only takes numbers in the id. Example of what I want to do: Var3f["Andrew"].char3f = "Value Assigned"; Var3f["Testint"].int3f = 12; //Assign "Andrew" to a array list for when the application closes it knows what id names to delete. Please say something if a part of my post isn't making sence to you. VS2005 STD Win32/C++ Windows XP

Share this post


Link to post
Share on other sites
I'm not sure if I get what your looking for but I'm assuming you want to assign string-id's to values, maybe for looking up these calues or whatever. You could either setup an array of structs wich would be static or look into std::map container.

Basically you'd have something like this with an array.


struct data
{
int value;
std::string id;
};

data ListOfValues[size];



Share this post


Link to post
Share on other sites
simply override the [] operator

Object& Collection::operator [] (char* id)
{
// scan/hash for object
if( strcmp( id, object.id ) == 0 )
return object;

// if no object found Assert or throw
}

Share this post


Link to post
Share on other sites
If you want to store heterogenous types, I believe you can use a
std::map<std::string, boost::any>

Which would then be used as such:
typedef std::map<std::string, boost::any> HeteroMap;

HeteroMap values;

values["Foo"] = 15;
values["Bar"] = std::string("London Bridge is falling down.");
values["Frob"] = 23.0f;

HeteroMap::iterator it = values.find("Foo");
if(it != values.end())
{
try
{
int anInt = boost::any_cast<int>(it->second);
assert(anInt == 15);
}
catch(const boost::bad_any_cast &e)
{
// "Foo" does not correspond to an int
}
}






I haven't actually done this myself, so feel free to correct me if I got something wrong.

Edit: Corrected myself. :) I'd dereferenced the iterator incorrectly.

[Edited by - scjohnno on June 23, 2008 4:37:43 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by scjohnno
If you want to store heterogenous types, I believe you can use a
std::map<std::string, boost::any>
QFE. This seems like your best option.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
I did try using map header, but I don't think I am using it properly since the id only takes numbers in the id.


std::maps don't just take numbers as their keys, they can take any comparable data type. In your case, the proposed std::map<std::string, boost::any> solution seems the best.

Share this post


Link to post
Share on other sites

This topic is 3462 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this