Computing the Median 1 : pair « Map Multimap « C++

Home
C++
 1 Bitset 2 Class 3 Console 4 Data Structure 5 Data Type 6 Deque 7 Development 8 File 9 Function 10 Generic 11 Language 12 List 13 Map Multimap 14 Overload 15 Pointer 16 Qt 17 Queue Stack 18 Set Multiset 19 STL Algorithms Binary search 20 STL Algorithms Heap 21 STL Algorithms Helper 22 STL Algorithms Iterator 23 STL Algorithms Merge 24 STL Algorithms Min Max 25 STL Algorithms Modifying sequence operations 26 STL Algorithms Non modifying sequence operations 27 STL Algorithms Sorting 28 STL Basics 29 String 30 Valarray 31 Vector
 C++ » Map Multimap » pair Screenshots
Computing the Median 1
 ```   #include  #include  #include  #include  using namespace std; template  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 a, const pair 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 v( a, a + len );     vector::iterator v_end = v.end();     print( v );          map frequency;     for( vector::iterator i = v.begin(); i != v_end;++i )         ++frequency[*i];          pair mode_pair = *max_element( frequency.begin(),frequency.end(), second_less );          cout << "Mode by method 1: " << mode_pair.first;      }           ```
Related examples in the same category
 1 Inserting pairs of object into map 2 print the maximum number of pairs that DateMap can hold 3 Use map to store the value of the month name and its day number 4 Iterating over the elements of the map and using the current pair option and second elements. 5 Put pairs to map with insert 6 Map for string key and integer value 7 for each basic 8 Sort a list of int and string pairs 9 Pass output message function to for_each function