1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <bits/stdc++.h> using namespace std; #define M 100007 #define N 1007 #define INF 0x3f3f3f3f #define ll long long #define db double
void printMap(map<int, int>& m) { for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { printf("key:%d value:%d\n", it->first, it->second); } puts(""); } void build_map() { map<int, int> m1; m1.insert(pair<int, int>(1, 21373191)); m1.insert(pair<int, int>(3, 21373119)); m1.insert(pair<int, int>(2, 21373181)); m1.insert(pair<int, int>(4, 21373190)); m1.insert(pair<int, int>(3, 21373190)); printMap(m1); map<int, int> m2(m1); printMap(m2); map<int, int> m3; m3 = m1; printMap(m3); } void size_map() { map<int, int> m1; m1.insert(pair<int, int>(1, 21373191)); m1.insert(pair<int, int>(3, 21373119)); m1.insert(pair<int, int>(2, 21373181)); m1.insert(pair<int, int>(4, 21373190)); printMap(m1); if (m1.empty()) { printf("m1为空\n"); } else { printf("m1不为空\n"); printf("大小:%d\n", m1.size()); } map<int, int> m2; m2.insert(pair<int, int>(4, 191)); m2.insert(pair<int, int>(3, 119)); m2.insert(pair<int, int>(5, 181)); m2.insert(pair<int, int>(6, 190)); m1.swap(m2); printMap(m1); printMap(m2); } void calc_map() { map<int, int> m1; m1.insert(pair<int, int>(1, 21373191)); m1.insert(make_pair(3, 21373119)); m1.insert(map<int, int>::value_type(2, 21373181)); m1[4] = 21373190; printMap(m1);
m1.erase(m1.begin()); map<int, int>::iterator it = m1.begin(); it++; m1.erase(++it, m1.end()); m1.erase(2); printMap(m1); m1.clear(); } void statistics_map() { map<int, int> m; m.insert(pair<int, int>(1, 21373191)); m.insert(pair<int, int>(3, 21373119)); m.insert(pair<int, int>(2, 21373181)); m.insert(pair<int, int>(4, 21373190)); map<int, int>::iterator pos = m.find(3); if (pos != m.end()) { printf("key:%d value:%d\n", pos->first, pos->second); } else { printf("not discover\n"); } int num = m.count(2); printf("%d\n", num); } class cmp { public: bool operator()(int v1, int v2) { return v1 > v2; } }; void sort_map() { map<int, int> m1; m1.insert(pair<int, int>(1, 21373191)); m1.insert(pair<int, int>(3, 21373119)); m1.insert(pair<int, int>(2, 21373181)); m1.insert(pair<int, int>(4, 21373190)); for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) { printf("key:%d value:%d\n", it->first, it->second); } puts(""); map<int, int, cmp> m2; m2.insert(pair<int, int>(1, 21373191)); m2.insert(pair<int, int>(3, 21373119)); m2.insert(pair<int, int>(2, 21373181)); m2.insert(pair<int, int>(4, 21373190)); for (map<int, int, cmp>::iterator it = m2.begin(); it != m2.end(); it++) { printf("key:%d value:%d\n", it->first, it->second); } puts(""); } int main() { statistics_map(); return 0; }
|