STL Equiv to MFC CMap? 
Author Message
 STL Equiv to MFC CMap?


Quote:
> What is the closest thing in STL to a CMap?  Is it map?  I've looked
at
> map and the insert method looks a little funky.  I'm not sure how to
use
> it.  All I want is to associate a std::string with an int key, adding
> them into the map one at a time, and then retrieving the std::string
> given the int key (which is actually an enum.)

> I was thinking of:
> map<int, std::string> m_MyMap;

> How do I add to the map?

typedef map<int, std::string> MyMap;
int key;
string value;

//This inserts value under key, or replaces the previous value if any
m_myMap[key] = value;

// This inserts the value under key if none is present, but does not
replace existing value
// It returns an indicator that lets you know which situation actually
happened
pair<MyMap::iterator, bool> ret =
    m_myMap.insert(MyMap::value_type(key, value));
if (ret.second)
{
    // New element was inserted at ret.first

Quote:
}

else
{
    // Old element is present at ret.first and was not replaced
    // If you want, you can still replace it with
    ret.first->second = value;

Quote:
}
> How do I get the string value from the map given the int key?

// If no value is stored under key, empty string is created and inserted
// Most of the time, you don't want this, so use this method only when
// you are sure the element is there.
string value = m_myMap[key];

// This piece checks whether the element is present, and retrieves the
value if so
MyMap::iterator it = m_myMap.find(key);
if (it != m_myMap.end())
{
    string value = it->second;

Quote:
}

else
{
    // The element is not there

Quote:
}

--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Tue, 12 Apr 2005 02:30:41 GMT  
 STL Equiv to MFC CMap?
 map<int, std::string> m_MyMap;

    m_MyMap[11] = "Hello";
    m_MyMap[2222] = "World";

    cout << m_MyMap[2222] << endl;

--
Truth,
James Curran
www.NovelTheory.com  (Personal)
www.NJTheater.com   (Professional)
www.aurora-inc.com   (Day job)


Quote:
> HI All,

> What is the closest thing in STL to a CMap?  Is it map?  I've looked at
> map and the insert method looks a little funky.  I'm not sure how to use
> it.  All I want is to associate a std::string with an int key, adding
> them into the map one at a time, and then retrieving the std::string
> given the int key (which is actually an enum.)

> I was thinking of:
> map<int, std::string> m_MyMap;

> How do I add to the map?
> How do I get the string value from the map given the int key?

> Thanks in advance,

> --Ed



Tue, 12 Apr 2005 02:21:26 GMT  
 STL Equiv to MFC CMap?
HI All,

What is the closest thing in STL to a CMap?  Is it map?  I've looked at
map and the insert method looks a little funky.  I'm not sure how to use
it.  All I want is to associate a std::string with an int key, adding
them into the map one at a time, and then retrieving the std::string
given the int key (which is actually an enum.)

I was thinking of:
map<int, std::string> m_MyMap;

How do I add to the map?
How do I get the string value from the map given the int key?

Thanks in advance,

--Ed



Tue, 12 Apr 2005 01:40:48 GMT  
 STL Equiv to MFC CMap?
James,

When I try to extract info from the map using m_MyMap[2222] I get the
following compile error:

c:\...\UdlInfo.cpp(27) : error C2678: binary '[' : no operator found
which takes a left-hand operand of type 'const
std::map<_Kty,_Ty,_Pr,_Alloc>' (or there is no acceptable conversion)
         with
         [
             _Kty=int,
             _Ty=std::string,
             _Pr=std::less<int>,
             _Alloc=std::allocator<std::pair<const int,std::string>>
         ]

Is there another easy way to get the value from the map given the key?

--Ed

Quote:

>  map<int, std::string> m_MyMap;

>     m_MyMap[11] = "Hello";
>     m_MyMap[2222] = "World";

>     cout << m_MyMap[2222] << endl;

> --
> Truth,
> James Curran
> www.NovelTheory.com  (Personal)
> www.NJTheater.com   (Professional)
> www.aurora-inc.com   (Day job)



>>HI All,

>>What is the closest thing in STL to a CMap?  Is it map?  I've looked at
>>map and the insert method looks a little funky.  I'm not sure how to use
>>it.  All I want is to associate a std::string with an int key, adding
>>them into the map one at a time, and then retrieving the std::string
>>given the int key (which is actually an enum.)

>>I was thinking of:
>>map<int, std::string> m_MyMap;

>>How do I add to the map?
>>How do I get the string value from the map given the int key?

>>Thanks in advance,

>>--Ed



Tue, 12 Apr 2005 05:31:30 GMT  
 STL Equiv to MFC CMap?
Not as nice with a constant map:

#include <map>
#include <string>

int main(int argc, char* argv[])
{
    std::map<int, std::string> MyMap;
    const std::map<int, std::string>& MyConstMap = MyMap;
    MyMap[11] = "hello";
    MyMap[2222] = "World";

    //std::string foo = MyConstMap[2222];

    std::string foo = MyConstMap.find(2222)->second;

    return 0;

Quote:
}



Quote:
> James,

> When I try to extract info from the map using m_MyMap[2222] I get the
> following compile error:

> c:\...\UdlInfo.cpp(27) : error C2678: binary '[' : no operator found
> which takes a left-hand operand of type 'const
> std::map<_Kty,_Ty,_Pr,_Alloc>' (or there is no acceptable conversion)
>          with
>          [
>              _Kty=int,
>              _Ty=std::string,
>              _Pr=std::less<int>,
>              _Alloc=std::allocator<std::pair<const int,std::string>>
>          ]

> Is there another easy way to get the value from the map given the key?


> >  map<int, std::string> m_MyMap;

> >     m_MyMap[11] = "Hello";
> >     m_MyMap[2222] = "World";

> >     cout << m_MyMap[2222] << endl;



> >>HI All,

> >>What is the closest thing in STL to a CMap?  Is it map?  I've looked at
> >>map and the insert method looks a little funky.  I'm not sure how to use
> >>it.  All I want is to associate a std::string with an int key, adding
> >>them into the map one at a time, and then retrieving the std::string
> >>given the int key (which is actually an enum.)

> >>I was thinking of:
> >>map<int, std::string> m_MyMap;

> >>How do I add to the map?
> >>How do I get the string value from the map given the int key?



Tue, 12 Apr 2005 06:18:56 GMT  
 STL Equiv to MFC CMap?
I should point out that this isn't a very safe example in that, if 2222 were
not in the map, this code will crash.

to be on the safe side:

const std::map<int, std::string>::iterator it = MyConstMap.find(2222);

if (it != MyConstMap.end())
{
// safe to use it

Quote:
}

else
{
// not found!

Quote:
}



Quote:
> Not as nice with a constant map:

> #include <map>
> #include <string>

> int main(int argc, char* argv[])
> {
>     std::map<int, std::string> MyMap;
>     const std::map<int, std::string>& MyConstMap = MyMap;
>     MyMap[11] = "hello";
>     MyMap[2222] = "World";

>     //std::string foo = MyConstMap[2222];

>     std::string foo = MyConstMap.find(2222)->second;

>     return 0;
> }



> > James,

> > When I try to extract info from the map using m_MyMap[2222] I get the
> > following compile error:

> > c:\...\UdlInfo.cpp(27) : error C2678: binary '[' : no operator found
> > which takes a left-hand operand of type 'const
> > std::map<_Kty,_Ty,_Pr,_Alloc>' (or there is no acceptable conversion)
> >          with
> >          [
> >              _Kty=int,
> >              _Ty=std::string,
> >              _Pr=std::less<int>,
> >              _Alloc=std::allocator<std::pair<const int,std::string>>
> >          ]

> > Is there another easy way to get the value from the map given the key?


> > >  map<int, std::string> m_MyMap;

> > >     m_MyMap[11] = "Hello";
> > >     m_MyMap[2222] = "World";

> > >     cout << m_MyMap[2222] << endl;



> > >>HI All,

> > >>What is the closest thing in STL to a CMap?  Is it map?  I've looked
at
> > >>map and the insert method looks a little funky.  I'm not sure how to
use
> > >>it.  All I want is to associate a std::string with an int key, adding
> > >>them into the map one at a time, and then retrieving the std::string
> > >>given the int key (which is actually an enum.)

> > >>I was thinking of:
> > >>map<int, std::string> m_MyMap;

> > >>How do I add to the map?
> > >>How do I get the string value from the map given the int key?



Tue, 12 Apr 2005 06:24:04 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. STL vs MFC CMap, CArray, CList

2. STL vs. MFC CMap, CArray, CList

3. MFC equiv. for these API calls

4. whats the equiv of argv[0] in mfc?

5. C++ / MFC equiv of VBA's GetObject() ?

6. mfc equiv for atl construct

7. CMap containing another CMap

8. CMAP or CMAP like functionality -- Hash Tables

9. MFC + STL < STL

10. MFC + STL doesn't work (STL - MFC works fine)?

11. STL, MFC, ATL

12. ATL+WTL+STL versus MFC

 

 
Powered by phpBB® Forum Software