Created
February 15, 2017 15:02
-
-
Save fpdjsns/b93bc31296bd3444a4d42ec1f37d70d3 to your computer and use it in GitHub Desktop.
How to use map and what effect it is.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<map> | |
#include<functional> | |
using namespace std; | |
//print map element | |
void print(map<int, char> m) | |
{ | |
//empth function | |
if (m.empty())//nothing in the m | |
{ | |
cout << "nothing" << endl; | |
return; | |
} | |
//using iterator | |
map<int, char>::iterator iter; | |
for (iter = m.begin(); iter != m.end(); ++iter) | |
{ | |
cout << iter->first << " " << iter->second << endl; | |
} | |
cout << endl; | |
} | |
int main() | |
{ | |
map<int, char> m; //key type : int, value type : char | |
//Insert (1,'a') to map | |
printf("insert (1,a)\n"); | |
m[1] = 'a'; //If a key value of 1 exists, it is updated. Otherwise, add to map. | |
print(m); | |
//insert function | |
pair<map<int, char>::iterator, bool> success; | |
success = m.insert(pair<int, char>(0, 'b')); //If it succeeds, return true. Otherwise return false. | |
if (success.second == true) | |
printf("insert (%d,%c)\n", success.first->first, success.first->second); | |
pair<int, char> temp; //Create an object to insert first | |
temp = make_pair(2, 'c'); | |
m.insert(temp); | |
printf("insert (2,c)\n"); | |
print(m); | |
//count | |
cout << "m.count(1) : " << m.count(1) << endl; //Number of key values in map 1 | |
cout << "m.count(5) : " << m.count(5) << endl; //Number of key values in map 5 | |
//emplace | |
cout << endl << "m.emplace (3,e)" << endl; | |
m.emplace(pair<int, char>(3, 'e')); | |
print(m); | |
//erase | |
cout << "erase(3)"<<endl; | |
m.erase(3); | |
print(m); | |
//find,lower_bound, upper_bound, | |
map<int, char>::iterator iter; | |
iter = m.find(1); | |
if(iter==m.end()) | |
printf("Can not find(1)\n\n"); | |
else | |
printf("find(1) : %c\n\n", iter->second); | |
map<int, char>::iterator upp; | |
map<int, char>::iterator low; | |
low = m.lower_bound(1); | |
upp = m.upper_bound(1); | |
printf("lower_bound(1) : (%d, %c)\n", low->first, low->second); | |
printf("upper_bound(1) : (%d, %c)\n", upp->first, upp->second); | |
cout << endl; | |
//size | |
cout<<"size of map : "<<m.size()<<endl; | |
cout << endl; | |
//greater sort | |
map<int, char, greater<int>> gre; | |
for (int i = 0; i < m.size(); i++) | |
gre[i] = m[i]; | |
map<int, char, greater<int>> ::iterator iter2; | |
cout << "greater<int>" << endl; | |
for (iter2 = gre.begin(); iter2 != gre.end(); ++iter2) | |
{ | |
cout << iter2->first << " " << iter2->second << endl; | |
} | |
cout << endl; | |
//print reverse | |
map<int, char> ::reverse_iterator riter; | |
cout << "print reverse" << endl; | |
for (riter = m.rbegin(); riter != m.rend(); ++riter) | |
cout << riter->first << " " << riter->second << endl; | |
cout << endl; | |
//swap | |
map<int, char> m2; | |
m2[0] = 'z'; | |
m.swap(m2); | |
cout << "swap(m2)" << endl; | |
print(m); | |
//clear | |
cout << "clear map" << endl; | |
m.clear(); | |
print(m); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment