how to use map in this way? 
Author Message
 how to use map in this way?

Hi,
I want to use map in my program but I don't want it sort for me and I would
like to have a NoCase comparation. Can I do that? how? or if I can use map,
can any other container provide map's key-value feature. I am using VC++
6.0. It seems hash_map can do this, but it is not included in VC stl
implementation.

Thanks



Sat, 01 Jan 2005 17:54:58 GMT  
 how to use map in this way?
Here is an example (I have used 'int' for value and 'std::string' for key):

struct NoCaseLess
{
    bool operator()(const string& rs1, const string& rs2) const
    { return stricmp(rs1.c_str(), rs2.c_str()) < 0; }

Quote:
};

typedef map<string, int, NoCaseLess> Cont;

Regards,
Boris


Quote:
> Hi,
> I want to use map in my program but I don't want it sort for me and I
would
> like to have a NoCase comparation. Can I do that? how? or if I can use
map,
> can any other container provide map's key-value feature. I am using VC++
> 6.0. It seems hash_map can do this, but it is not included in VC stl
> implementation.

> Thanks



Sat, 01 Jan 2005 22:07:54 GMT  
 how to use map in this way?
yes. the nocase compare works. Thanks. And how to do no-sort, which mean the
element follow the sequence of what it is added in the map.

Thanks.



Quote:
> Here is an example (I have used 'int' for value and 'std::string' for
key):

> struct NoCaseLess
> {
>     bool operator()(const string& rs1, const string& rs2) const
>     { return stricmp(rs1.c_str(), rs2.c_str()) < 0; }
> };

> typedef map<string, int, NoCaseLess> Cont;

> Regards,
> Boris



> > Hi,
> > I want to use map in my program but I don't want it sort for me and I
> would
> > like to have a NoCase comparation. Can I do that? how? or if I can use
> map,
> > can any other container provide map's key-value feature. I am using VC++
> > 6.0. It seems hash_map can do this, but it is not included in VC stl
> > implementation.

> > Thanks



Sun, 02 Jan 2005 11:36:57 GMT  
 how to use map in this way?
It is as simple as iterating each STL container:

typedef map<....> MyMap;
MyMap theMap;
..... //Fill the map
//Now standard iteration:
//(use 'iterator' instead of 'const_iterator' if you want to modify
elements):
for (MyMap::const_iterator it = theMap.begin(); it != theMap.end(); ++it)
{
   cout << "Key: " << it->first << " Value: " << it->second << endl;

Quote:
}

...
But note that it you use 'map', not 'multimap' container, you cannot add two
strings, which are are equal in no-case:
theMap.insert(make_pair("Boris", 2);
theMap.insert(make_pair("boris", 3);//This value won't be added in case of
map!!!!

If you want both values to be added just use multimap.

Regards,
Boris


Quote:
> yes. the nocase compare works. Thanks. And how to do no-sort, which mean
the
> element follow the sequence of what it is added in the map.

> Thanks.



> > Here is an example (I have used 'int' for value and 'std::string' for
> key):

> > struct NoCaseLess
> > {
> >     bool operator()(const string& rs1, const string& rs2) const
> >     { return stricmp(rs1.c_str(), rs2.c_str()) < 0; }
> > };

> > typedef map<string, int, NoCaseLess> Cont;

> > Regards,
> > Boris



> > > Hi,
> > > I want to use map in my program but I don't want it sort for me and I
> > would
> > > like to have a NoCase comparation. Can I do that? how? or if I can use
> > map,
> > > can any other container provide map's key-value feature. I am using
VC++
> > > 6.0. It seems hash_map can do this, but it is not included in VC stl
> > > implementation.

> > > Thanks



Sun, 02 Jan 2005 15:10:41 GMT  
 how to use map in this way?
Is this true? The map will sort the element added, so the output will not as
the same sequence as input. Can you give a solution about that? I only have
the idea of using vector instead of map, but the obvious problem is that I
can't use key-data pair with vector.

Quote:
> It is as simple as iterating each STL container:

> typedef map<....> MyMap;
> MyMap theMap;
> ..... //Fill the map
> //Now standard iteration:
> //(use 'iterator' instead of 'const_iterator' if you want to modify
> elements):
> for (MyMap::const_iterator it = theMap.begin(); it != theMap.end(); ++it)
> {
>    cout << "Key: " << it->first << " Value: " << it->second << endl;
> }

> ...
> But note that it you use 'map', not 'multimap' container, you cannot add
two
> strings, which are are equal in no-case:
> theMap.insert(make_pair("Boris", 2);
> theMap.insert(make_pair("boris", 3);//This value won't be added in case of
> map!!!!

> If you want both values to be added just use multimap.

> Regards,
> Boris



> > yes. the nocase compare works. Thanks. And how to do no-sort, which mean
> the
> > element follow the sequence of what it is added in the map.

> > Thanks.



> > > Here is an example (I have used 'int' for value and 'std::string' for
> > key):

> > > struct NoCaseLess
> > > {
> > >     bool operator()(const string& rs1, const string& rs2) const
> > >     { return stricmp(rs1.c_str(), rs2.c_str()) < 0; }
> > > };

> > > typedef map<string, int, NoCaseLess> Cont;

> > > Regards,
> > > Boris



> > > > Hi,
> > > > I want to use map in my program but I don't want it sort for me and
I
> > > would
> > > > like to have a NoCase comparation. Can I do that? how? or if I can
use
> > > map,
> > > > can any other container provide map's key-value feature. I am using
> VC++
> > > > 6.0. It seems hash_map can do this, but it is not included in VC stl
> > > > implementation.

> > > > Thanks



Sun, 02 Jan 2005 16:42:28 GMT  
 how to use map in this way?

Quote:

> And how to do no-sort, which mean the
> element follow the sequence of what it is added in the map.

std::map is intrinsically sorted.  It's necessary to meet the
performance requirements the standard imposes on the container.  Note
that a hash_map implementation will also be sorted, though not
necessarily in a logical order.

If you want an unsorted container, use std::vector, std::deque, or
std::list.  If you want to have a mapping as in std::map without having
the sorting or lookup complexity guarantees of std::map, you'll have
to roll your own.



Sun, 02 Jan 2005 22:11:59 GMT  
 how to use map in this way?
There is no way to un-sort a map. Maps are sorted - its the way they are.

If you want an unsorted list (or deque) of elements, but still have the
ability to index by a key, you could use 2 containers - store your values in
the list, and store an iterator to the list element in a map.

(don't do this using a vector - if you delete an item in a vector, it will
invalidate all iterators stored after the deleted item, whereas a list's
iterator will always be valid).

(as maps' iterators are valid even after deletion, you could store the
'index' of values in the vector and store your values in the map, as you
currently have implemented).

Cheers, Andy.


Quote:
> Is this true? The map will sort the element added, so the output will not
as
> the same sequence as input. Can you give a solution about that? I only
have
> the idea of using vector instead of map, but the obvious problem is that I
> can't use key-data pair with vector.

> > It is as simple as iterating each STL container:

> > typedef map<....> MyMap;
> > MyMap theMap;
> > ..... //Fill the map
> > //Now standard iteration:
> > //(use 'iterator' instead of 'const_iterator' if you want to modify
> > elements):
> > for (MyMap::const_iterator it = theMap.begin(); it != theMap.end();
++it)
> > {
> >    cout << "Key: " << it->first << " Value: " << it->second << endl;
> > }

> > ...
> > But note that it you use 'map', not 'multimap' container, you cannot add
> two
> > strings, which are are equal in no-case:
> > theMap.insert(make_pair("Boris", 2);
> > theMap.insert(make_pair("boris", 3);//This value won't be added in case
of
> > map!!!!

> > If you want both values to be added just use multimap.

> > Regards,
> > Boris



> > > yes. the nocase compare works. Thanks. And how to do no-sort, which
mean
> > the
> > > element follow the sequence of what it is added in the map.

> > > Thanks.



> > > > Here is an example (I have used 'int' for value and 'std::string'
for
> > > key):

> > > > struct NoCaseLess
> > > > {
> > > >     bool operator()(const string& rs1, const string& rs2) const
> > > >     { return stricmp(rs1.c_str(), rs2.c_str()) < 0; }
> > > > };

> > > > typedef map<string, int, NoCaseLess> Cont;

> > > > Regards,
> > > > Boris



> > > > > Hi,
> > > > > I want to use map in my program but I don't want it sort for me
and
> I
> > > > would
> > > > > like to have a NoCase comparation. Can I do that? how? or if I can
> use
> > > > map,
> > > > > can any other container provide map's key-value feature. I am
using
> > VC++
> > > > > 6.0. It seems hash_map can do this, but it is not included in VC
stl
> > > > > implementation.

> > > > > Thanks



Mon, 03 Jan 2005 00:22:19 GMT  
 how to use map in this way?


Quote:
> yes. the nocase compare works. Thanks. And how to do no-sort, which mean
the
> element follow the sequence of what it is added in the map.

You _CANNOT_ do it.

The containers set,multiset,map,multimap are always sorted. They have an
ordering property.
They will not ordered elements  based on the order they are insert()ed in.
Instead they are ordered based on the compare for the key (which defaults to
< operator). That is a fundamental property of how they work and cannot be
changed once you have an instance of the container.
If you break this then the container is broken and none of the builtin
methods will give the right results.

You want to consider one of vector,deque,list where push_back() which do not
sort the elements

Stephen Howe



Mon, 03 Jan 2005 00:45:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Problem with using map .erase inside a map loop

2. Event handler using a control variable and not using message map

3. Using stl::map with class as key

4. Q)Using stl map in an Atl project...

5. retrieve XML data from SQL server by using XSD mapping schema

6. using XSD as mapping schema

7. Using find on a map with custom comparitors

8. Using map.find() with pointers to strings

9. Unexplained warnings when using maps and strings

10. Error using Microsoft's implementation of std::map

11. problems with inserting objects, using maps

12. Creating maps using digital data...

 

 
Powered by phpBB® Forum Software