N
Naveen
I am trying to write a conatiner which has std::map like methods but
preserves the order of insertion.
TO achieve this I thought of providing my own function for comparing
the keys of the map. Following is the sample code for this:
template <class T>
struct my_compare
{
bool operator () (const T&a, const T&b)
{
return false;
}
};
int main()
{
std::map<std::string,int, my_compare<std::string> > a;
std::string str = "1";
a[str] = 1;
str = "3";
a[str] = 3;
str = "2";
a[str] = 2;
str = "5";
a[str] = 5;
int size = a.size();
std::map<std::string,int, my_compare<std::string> > ::iterator iter =
a.begin();
for(; iter != a.end(); ++iter)
{
cout<<iter->first.c_str() <<"\n";
}
return 1;
}
The output of this code is : 1 i.e. there is only one element in the
map. Can somebody explain why ?
If I return true in operator(), the output is 5 2 3 1 (as expected).
I am compiling it using VC 6 compiler.
preserves the order of insertion.
TO achieve this I thought of providing my own function for comparing
the keys of the map. Following is the sample code for this:
template <class T>
struct my_compare
{
bool operator () (const T&a, const T&b)
{
return false;
}
};
int main()
{
std::map<std::string,int, my_compare<std::string> > a;
std::string str = "1";
a[str] = 1;
str = "3";
a[str] = 3;
str = "2";
a[str] = 2;
str = "5";
a[str] = 5;
int size = a.size();
std::map<std::string,int, my_compare<std::string> > ::iterator iter =
a.begin();
for(; iter != a.end(); ++iter)
{
cout<<iter->first.c_str() <<"\n";
}
return 1;
}
The output of this code is : 1 i.e. there is only one element in the
map. Can somebody explain why ?
If I return true in operator(), the output is 5 2 3 1 (as expected).
I am compiling it using VC 6 compiler.