C++/Data Structure/Map
Содержание
- 1 A map: insert pair, find, end
- 2 A map of opposites.
- 3 A map of word opposites, using strings.
- 4 A simple map: char and int
- 5 [] automatically inserts elements.
- 6 Cycle through a map in reverse.
- 7 Cycle through a map using an iterator.
- 8 Demonstrating a multimap.
- 9 File and map
- 10 Maps can store only unique keys.
- 11 Use a map of strings to create a phone directory.
- 12 Use a map to create a phone directory: string class
- 13 Use a multimap to create the phone directory.
- 14 Use the greater function object in Map
- 15 Using [] in Map
A map: insert pair, find, end
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
int i;
for(i = 0; i <10; i++) {
mapObject.insert(pair<char, int>("A"+i, i));
}
char ch;
cout << "Enter key: ";
cin >> ch;
map<char, int>::iterator p;
p = mapObject.find(ch);
if(p != mapObject.end())
cout << p->second;
else
cout << "Key not in map.\n";
return 0;
}
A map of opposites.
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
class StringClass {
char str[20];
public:
StringClass() {
strcpy(str, "");
}
StringClass(char *s) {
strcpy(str, s);
}
char *get() {
return str;
}
};
// must define less than relative to StringClass objects
bool operator<(StringClass a, StringClass b)
{
return strcmp(a.get(), b.get()) < 0;
}
class opposite {
char str[20];
public:
opposite() {
strcmp(str, "");
}
opposite(char *s) {
strcpy(str, s);
}
char *get() {
return str;
}
};
int main()
{
map<StringClass, opposite> mapObject;
mapObject.insert(pair<StringClass, opposite>(StringClass("yes"), opposite("no")));
mapObject.insert(pair<StringClass, opposite>(StringClass("good"), opposite("bad")));
mapObject.insert(pair<StringClass, opposite>(StringClass("left"), opposite("right")));
mapObject.insert(pair<StringClass, opposite>(StringClass("up"), opposite("down")));
char str[80];
cout << "Enter word: ";
cin >> str;
map<StringClass, opposite>::iterator p;
p = mapObject.find(StringClass(str));
if(p != mapObject.end())
cout << "Opposite: " << p->second.get();
else
cout << "Word not in map.\n";
return 0;
}
A map of word opposites, using strings.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, string> mapObject;
int i;
mapObject.insert(pair<string, string>("yes", "no"));
mapObject.insert(pair<string, string>("up", "down"));
mapObject.insert(pair<string, string>("left", "right"));
mapObject.insert(pair<string, string>("good", "bad"));
string s;
cout << "Enter word: ";
cin >> s;
map<string, string>::iterator p;
p = mapObject.find(s);
if(p != mapObject.end())
cout << "Opposite: " << p->second;
else
cout << "Word not in map.\n";
return 0;
}
A simple map: char and int
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
int i;
for(i = 0; i <26; i++) {
mapObject.insert(pair<char, int>("A"+i, 65+i));
}
char ch;
cout << "Enter key: ";
cin >> ch;
map<char, int>::iterator p;
p = mapObject.find(ch);
if(p != mapObject.end())
cout << "Its ASCII value is " << p->second;
else
cout << "Key not in map.\n";
return 0;
}
[] automatically inserts elements.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
cout << "Initial size of map: " << mapObject.size() << endl;
cout << "The value associated with A is " << mapObject["A"];
cout << endl;
cout << "Size of map is now: " << mapObject.size() << endl;
return 0;
}
Cycle through a map in reverse.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
int i;
for(i = 0; i <26; i++)
mapObject.insert(pair<char, int>("A" + i, 65 + i));
map<char, int>::reverse_iterator p;
for(p = mapObject.rbegin(); p != mapObject.rend(); p++) {
cout << p->first << " has ASCII value of ";
cout << p->second << endl;
}
return 0;
}
Cycle through a map using an iterator.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
int i;
for(i = 0; i < 26; i++)
mapObject.insert(pair<char, int>("A" + i, 65 + i));
map<char, int>::iterator p;
for(p = mapObject.begin(); p != mapObject.end(); p++) {
cout << p->first << " has ASCII value of ";
cout << p->second << endl;
}
return 0;
}
Demonstrating a multimap.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
multimap<string, string> names;
string n;
names.insert(pair<string, string>("Z", "F"));
names.insert(pair<string, string>("Z", "A"));
names.insert(pair<string, string>("S", "T"));
names.insert(pair<string, string>("S", "A"));
names.insert(pair<string, string>("S", "J"));
names.insert(pair<string, string>("D", "H"));
names.insert(pair<string, string>("D", "W"));
names.insert(pair<string, string>("D", "R"));
multimap<string, string>::iterator p;
cout << "Enter last name: ";
cin >> n;
p = names.find(n);
if(p != names.end()) { // found a name
do {
cout << n << ", " << p->second;
cout << endl;
p++;
} while (p != names.upper_bound(n));
}
else{
cout << "Name not found.\n";
}
return 0;
}
File and map
#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;
int main()
{
map<string, int> hist;
ifstream in( "inFile" );
string word;
while ( in >> word )
hist[ word ]++;
in.close();
typedef map<string, int>::const_iterator CI;
for ( CI iter = hist.begin(); iter != hist.end(); ++iter )
cout << iter->first << "\t" << iter->second << endl;
return 0;
}
Maps can store only unique keys.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
pair<map<char,int>::iterator, bool> res;
res = mapObject.insert(pair<char, int>("A", 65));
if(res.second)
cout << "Insertion occured.\n";
res = mapObject.insert(pair<char, int>("A", 99));
if(!res.second)
cout << "Duplicate not allowed.\n";
map<char, int>::iterator p;
p = mapObject.find("A");
cout << "Its ASCII value is " << p->second;
return 0;
}
Use a map of strings to create a phone directory.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, string> directory;
directory.insert(pair<string, string>("T", "4444"));
directory.insert(pair<string, string>("C", "9999"));
directory.insert(pair<string, string>("J", "8888"));
directory.insert(pair<string, string>("R", "0000"));
string s;
cout << "Enter name: ";
cin >> s;
map<string, string>::iterator p;
p = directory.find(s);
if(p != directory.end())
cout << "Phone number: " << p->second;
else
cout << "Name not in directory.\n";
return 0;
}
Use a map to create a phone directory: string class
#include <iostream>
#include <map>
#include <string>
using namespace std;
class name {
string str;
public:
name() {
str = "";
}
name(string s) {
str = s;
}
string get() {
return str;
}
};
// Define less than relative to name objects.
bool operator<(name a, name b){
return a.get() < b.get();
}
class phoneNum {
string str;
public:
phoneNum() {
str = "";
}
phoneNum(string s) {
str = s;
}
string get() {
return str;
}
};
int main()
{
map<name, phoneNum> directory;
directory.insert(pair<name, phoneNum>(name("J"), phoneNum("999-1111")));
directory.insert(pair<name, phoneNum>(name("C"), phoneNum("999-2222")));
directory.insert(pair<name, phoneNum>(name("J"), phoneNum("555-3333")));
directory.insert(pair<name, phoneNum>(name("T"),phoneNum("4444")));
// given a name, find number
string str;
cout << "Enter name: ";
cin >> str;
map<name, phoneNum>::iterator p;
p = directory.find(name(str));
if(p != directory.end())
cout << "Phone number: " << p->second.get();
else
cout << "Name not in directory.\n";
return 0;
}
Use a multimap to create the phone directory.
#include <iostream>
#include <map>
#include <string>
using namespace std;
class name {
string str;
public:
name() {
str = "";
}
name(string s) {
str = s;
}
string get() {
return str;
}
};
bool operator<(name a, name b) { // Define less than relative to name objects.
return a.get() < b.get();
}
class phoneNum {
string str;
public:
phoneNum() {
str = "";
}
phoneNum(string s) {
str = s;
}
string get() {
return str;
}
};
int main()
{
multimap<name, phoneNum> directory;
directory.insert(pair<name, phoneNum>(name("T"), phoneNum("555-4533")));
directory.insert(pair<name, phoneNum>(name("T"), phoneNum("555-9999")));
directory.insert(pair<name, phoneNum>(name("C"), phoneNum("555-9678")));
string str;
cout << "Enter name: ";
cin >> str;
multimap<name, phoneNum>::iterator p;
p = directory.find(str);
if(p != directory.end()) {
do {
cout << "Phone number: " << p->second.get();
cout << endl;
p++;
} while(p != directory.upper_bound(str));
}
else
cout << "Name not in directory.\n";
return 0;
}
Use the greater function object in Map
#include <iostream>
#include <map>
#include <functional>
#include <string>
using namespace std;
int main()
{
map<string, int, greater<string> > mapObject;
mapObject["A"] = 20;
mapObject["B"] = 19;
mapObject["C"] = 10;
map<string, int, greater<string> >::iterator p;
for(p = mapObject.begin(); p != mapObject.end(); p++) {
cout << p->first << " has value of ";
cout << p->second << endl;
}
return 0;
}
Using [] in Map
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
int i;
for(i = 0; i < 26; i++)
mapObject.insert(pair<char, int>("A" + i, 65 + i));
char ch;
cout << "Enter key: ";
cin >> ch;
cout << "Its ASCII value is " << mapObject[ ch ];
return 0;
}