C++/Map Multimap/pair
Содержание
- 1 Computing the Median 1
- 2 for each basic
- 3 Inserting pairs of object into map
- 4 Iterating over the elements of the map and using the current pair option and second elements.
- 5 Map for string key and integer value
- 6 Pass output message function to for_each function
- 7 print the maximum number of <key,data> pairs that DateMap can hold
- 8 Put pairs to map with insert
- 9 Sort a list of int and string pairs
- 10 Use map to store the value of the month name and its day number
Computing the Median 1
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
template <class T>
void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
}
inline
bool second_less( const pair<int,int> a, const pair<int,int> b )
{
return a.second < b.second;
}
int main( )
{
const int len = 15;
const int a[len] = { 9, 2, 3, 3, 7, 5, 7, 7, 4, 10, 5, 6, 7, 4, 7 };
vector<int> v( a, a + len );
vector<int>::iterator v_end = v.end();
print( v );
map<int,int> frequency;
for( vector<int>::iterator i = v.begin(); i != v_end;++i )
++frequency[*i];
pair<int,int> mode_pair = *max_element( frequency.begin(),frequency.end(), second_less );
cout << "Mode by method 1: " << mode_pair.first;
}
for each basic
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
void printPair(const pair<int, int>& elem)
{
cout << elem.first << "->" << elem.second << endl;
}
int main(int argc, char** argv)
{
map<int, int> myMap;
myMap.insert(make_pair(4, 40));
myMap.insert(make_pair(5, 50));
myMap.insert(make_pair(6, 60));
myMap.insert(make_pair(7, 70));
myMap.insert(make_pair(8, 80));
for_each(myMap.begin(), myMap.end(), &printPair);
return (0);
}
Inserting pairs of object into map
#include <map>
#include <iostream>
#include <string>
using namespace std;
int main()
{
map<string, double> option;
option["r"] = 0.1;
option["s"] = 0.2;
option["K"] = 1.0;
option["T"] = 0.7;
option["S"] = 1.3;
cout << "Size of map: " << option.size() << endl;;
map<string, double>::iterator i = option.begin();
while (i != option.end())
{
cout << "Element pair [" << (*i).first << "," << (*i).second << "]";
i++;
}
return 0;
}
Iterating over the elements of the map and using the current pair option and second elements.
#include <map>
#include <iostream>
#include <string>
using namespace std;
int main()
{
map<string, double> option;
option["r"] = 0.1;
option["s"] = 0.2;
option["K"] = 1.0;
option["T"] = 0.7;
option["S"] = 1.3;
cout << "Size of map: " << option.size() << endl;;
map<string, double>::iterator i = option.begin();
while (i != option.end())
{
cout << "Element pair [" << (*i).first << "," << (*i).second << "]";
i++;
}
return 0;
}
Map for string key and integer value
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, int, less<string> > name_age;
name_age["A"] = 7;
name_age["B"] = 39;
name_age["C"] = 14;
cout << "A is "
<< name_age["A"]
<< " years old." << endl;
}
Pass output message function to for_each function
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <utility>
using namespace std;
void print_message( const pair<int,string>& message );
int main( )
{
list< pair< int, string > > message;
message.push_back( make_pair( 1, "A" ) );
message.push_back( make_pair( 2, "B" ) );
message.push_back( make_pair( 3, "C" ) );
for_each( message.begin(), message.end(), print_message );
}
void print_message( const pair<int,string>& message ){
cout << message.first << " - " << message.second << endl;
}
print the maximum number of <key,data> pairs that DateMap can hold
#include <map>
#include <iostream>
#include <string>
using namespace std;
typedef map<string, int> STRING2INT;
int main(void)
{
STRING2INT DateMap;
STRING2INT::iterator DateIterator;
string DateBuffer;
cout << "DateMap is capable of holding " << DateMap.max_size()
<< " <string,int> pairs" << endl;
}
Put pairs to map with insert
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> m;
int i;
for(i=0; i<26; i++) {
m.insert(pair<char, int>("A"+i, 65+i));
}
return 0;
}
Sort a list of int and string pairs
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <utility>
using namespace std;
void print_message( const pair<int,string>& message );
int main( )
{
list< pair< int, string > > message;
message.push_back( make_pair( 1, "A" ) );
message.push_back( make_pair( 2, "B" ) );
message.push_back( make_pair( 3, "C" ) );
for_each( message.begin(), message.end(), print_message );
message.sort();
for_each( message.begin(), message.end(), print_message );
}
void print_message( const pair<int,string>& message ){
cout << message.first << " - " << message.second << endl;
}
Use map to store the value of the month name and its day number
#include <map>
#include <iostream>
#include <string>
using namespace std;
class ltstr{
public:
bool operator()(const char* s1, const char* s2) const
{ return (strcmp(s1, s2) < 0);}
};
int main(void)
{
map<const char*, int, ltstr> months;
months["January"] = 31;
months["February"] = 28;
months["March"] = 31;
months["April"] = 30;
months["May"] = 31;
months["June"] = 30;
months["July"] = 31;
months["August"] = 31;
months["September"] = 30;
months["October"] = 31;
months["November"] = 30;
months["December"] = 31;
cout << "june -> " << months["June"] << endl;
map<const char*, int, ltstr>::iterator cur = months.find("June");
map<const char*, int, ltstr>::iterator prev = cur;
map<const char*, int, ltstr>::iterator next = cur;
++next;
--prev;
cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
cout << "Next (in alphabetical order) is " << (*next).first << endl;
}