Re: Maps



"Alamelu" wrote:
This is my scenario...

I have unsigned char constants defined in my class...

const unsigned char strKey1[] = "Key1";
const unsigned char strValue1[] = "Value1";

const unsigned char strKey2[] = "Key2";
const unsigned char strValue2[] = "Value2";

I want these data to be populated in a map..
If Key1 .. i should retrieve Value1...
If Key2 .. i should retrieve Value2...

How do i code this...???????


You can put pointers to char into map. However, you will
need to ensure that memory occupied by strings will remain
valid for lifetime of the map. Much simpler is to use
std::string class:

--------------------------------------------------------
typedef std::map<std::string, const std::string> STRMAP;

int main()
{
STRMAP m1;
m1.insert(STRMAP::value_type("key1", "one"));
m1.insert(STRMAP::value_type("key2", "two"));
m1.insert(STRMAP::value_type("key3", "three"));

const STRMAP m2(m1);

// retrieve value
STRMAP::const_iterator it = m2.find("key1");

if(it != m2.end())
std::cout << (*it).second;
else
std::cout << "not found";

return 0;
}
--------------------------------------------------------

HTH
Alex


.



Relevant Pages

  • Re: Collection vs well defined transfer objects [LONG]
    ... >object in the Map or two different Integer objects in the Map. ... Some of those items must by definition identify the function/service ... A factory can take the parameter, and create a Command object that will ... Each Command object will retrieve those items in the map that relate ...
    (comp.object)
  • Re: Ifstream problem
    ... Thanks a lot..But How I will retrieve the specific values based on the ... header string from the file?.I thought of Map will do the needful. ...
    (comp.lang.cpp)
  • Re: Map.get(key) returns null after inserting key
    ... I am inserting a key and a object into a Map. ... to retrieve the object by calling ...
    (comp.lang.java.programmer)
  • Re: Ifstream problem
    ... Thanks a lot..But How I will retrieve the specific values based on the ... header string from the file?.I thought of Map will do the needful. ...
    (comp.programming)

Loading