hey i wanna sort ma map using the second value which is an int.help me do this
Map:
[code=cpp]
class MyMap
{
public:
typedef string KeyType;
typedef int ValueType;
typedef pair<KeyType, ValueType> ItemType;
private:
typedef map<KeyType, ValueType> MapType;
MapType map_;
public:
bool AddItem( KeyType const & k, ValueType const & v )
{
// insert returns a std::pair of the position
// and whether the insertion succeeded.
// We are only interested in the success value.
return map_.insert( make_pair(k,v) ).second;
}
bool comp_int(const int &s1, const int &s2)
{
return(s1>s2 ? true:false);
}
bool Find( KeyType const & k, ValueType & v )
{
//sort(map_.begin (),map_.end());//needs to be sorted before the search happens.sorted in ascending order.the int value(i.e the v should be used for sorting)
MapType::iterat or pos( map_.find(k) );
if ( pos != map_.end() )
{
v = pos->second;
return true;
}
else
{
return false;
}
}
};
[/code]
Main:
[code=cpp]
int main()
{
MyMap p1;
for(int i=0;i<5;i++)
{
cout << "enter mac_addr value pair: ";
MyMap::KeyType key;
MyMap::ValueTyp e value;
cin >> key;
cin >> value;
p1.AddItem(key, value) ;
//{
// cerr << "Mac address duplicate, data not stored." << endl;
//}
}
cout << "Enter mac address to lookup: ";
MyMap::KeyType searchKey;
cin >> searchKey;
MyMap::ValueTyp e foundValue;
if ( p1.Find( searchKey, foundValue) )
{
cout << "Found value " << foundValue << " for search key " << searchKey << endl;
}
else
{
p1.AddItem(sear chKey,0);
cout << "No such mac address as " << searchKey << " in map." << endl;
}
return 0;
}
[/code]
//in this u have to sort the map inoder to compare properly.coz in this what happpens is when u insert the lastest ack,it is stored behind coz first in first out concept.so when it searches it encounters the first inserted sender and ack which mite be incorrect.
//so c if u can sort the map
Map:
[code=cpp]
class MyMap
{
public:
typedef string KeyType;
typedef int ValueType;
typedef pair<KeyType, ValueType> ItemType;
private:
typedef map<KeyType, ValueType> MapType;
MapType map_;
public:
bool AddItem( KeyType const & k, ValueType const & v )
{
// insert returns a std::pair of the position
// and whether the insertion succeeded.
// We are only interested in the success value.
return map_.insert( make_pair(k,v) ).second;
}
bool comp_int(const int &s1, const int &s2)
{
return(s1>s2 ? true:false);
}
bool Find( KeyType const & k, ValueType & v )
{
//sort(map_.begin (),map_.end());//needs to be sorted before the search happens.sorted in ascending order.the int value(i.e the v should be used for sorting)
MapType::iterat or pos( map_.find(k) );
if ( pos != map_.end() )
{
v = pos->second;
return true;
}
else
{
return false;
}
}
};
[/code]
Main:
[code=cpp]
int main()
{
MyMap p1;
for(int i=0;i<5;i++)
{
cout << "enter mac_addr value pair: ";
MyMap::KeyType key;
MyMap::ValueTyp e value;
cin >> key;
cin >> value;
p1.AddItem(key, value) ;
//{
// cerr << "Mac address duplicate, data not stored." << endl;
//}
}
cout << "Enter mac address to lookup: ";
MyMap::KeyType searchKey;
cin >> searchKey;
MyMap::ValueTyp e foundValue;
if ( p1.Find( searchKey, foundValue) )
{
cout << "Found value " << foundValue << " for search key " << searchKey << endl;
}
else
{
p1.AddItem(sear chKey,0);
cout << "No such mac address as " << searchKey << " in map." << endl;
}
return 0;
}
[/code]
//in this u have to sort the map inoder to compare properly.coz in this what happpens is when u insert the lastest ack,it is stored behind coz first in first out concept.so when it searches it encounters the first inserted sender and ack which mite be incorrect.
//so c if u can sort the map
Comment