赞
踩
-
- // map_insert_find_end.cpp
- // compile with: /EHsc
- #pragma warning(disable:4786)
- #include <iostream>
- #include <string>
- #include <map>
-
- using namespace std;
-
- typedef map<int, string, less<int> > INT2STRING;
-
- int main()
- {
- // 1. Create a map of ints to strings
- INT2STRING theMap;
- INT2STRING::iterator theIterator;
- string theString = "";
- int index;
-
- // Fill it with the digits 0 - 9, each mapped to its string counterpart
- // Note: value_type is a pair for maps...
- theMap.insert(INT2STRING::value_type(0,"Zero"));
- theMap.insert(INT2STRING::value_type(1,"One"));
- theMap.insert(INT2STRING::value_type(2,"Two"));
- theMap.insert(INT2STRING::value_type(3,"Three"));
- theMap.insert(INT2STRING::value_type(4,"Four"));
- theMap.insert(INT2STRING::value_type(5,"Five"));
- theMap.insert(INT2STRING::value_type(6,"Six"));
- theMap.insert(INT2STRING::value_type(7,"Seven"));
- theMap.insert(INT2STRING::value_type(8,"Eight"));
- theMap.insert(INT2STRING::value_type(9,"Nine"));
-
- // Read a Number from the user and print it back as words
- for( ; ; )
- {
- cout << "Enter \"q\" to quit, or enter a Number: ";
- cin >> theString;
- if (theString == "q")
- break;
-
- // extract each digit from the string, find its corresponding
- // entry in the map (the word equivalent) and print it
- for (index = 0; index < (signed)theString.length(); index++)
- {
- theIterator = theMap.find(theString[index] - '0');
- if (theIterator != theMap.end() ) // is 0 - 9
- cout << (*theIterator).second << " ";
- else // some character other than 0 - 9
- cout << "[err] ";
- }
- cout << endl;
- }
- }

Input
- // map_max_size_etc_sample.cpp
- // compile with: /EHsc
- //
- // Functions: iterator map::max_size();
- // void clear() const;
- // bool empty() const;
- // iterator erase(iterator first, iterator last);
- // size_type size() const;
- // A::reference operator[](const Key& key);
- // iterator map::begin();
- // iterator map::end();
- // iterator map::find(const Key& key);
-
- #pragma warning(disable:4786)
-
- #include <iostream>
- #include <string>
- #include <map>
-
- using namespace std ;
-
- typedef map<string, int> STRING2INT;
-
- int main()
- {
- STRING2INT MyMap;
- STRING2INT::iterator MyIterator;
- string MyBuffer;
-
- // print the maximum number of <key,data> pairs that MyMap can hold
- cout << "MyMap is capable of holding " << MyMap.max_size()
- << " <string,int> pairs" << endl;
-
- if (!MyMap.empty())
- cout << "MyMap has " << MyMap.size() << " entries" << endl;
- else
- cout << "MyMap is empty" << endl;
-
- cout << "Entering new entries in MyMap" << endl;
- // Fill in MyMap with the months of the year, mapped to their number
- // January - 1, February - 2, etc. using operator[].
- MyMap["January"] = 1;
- MyMap["February"] = 2;
- MyMap["March"] = 3;
- MyMap["April"] = 4;
- MyMap["May"] = 5;
- MyMap["June"] = 6;
- MyMap["July"] = 7;
- MyMap["August"] = 8;
- MyMap["September"] = 9;
- MyMap["October"] = 10;
- MyMap["November"] = 11;
- MyMap["December"] = 12;
-
- if (!MyMap.empty())
- cout << "MyMap has " << MyMap.size() << " entries" << endl;
- else
- cout << "MyMap is empty" << endl;
-
- // Ask the user for a month of the year and print the number
- // that corresponds to the month entered
- MyIterator = MyMap.end();
- while (MyIterator == MyMap.end())
- {
- cout << endl << "Enter a Month: ";
- cin >> MyBuffer;
- if ((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
- cout << (*MyIterator).first << " is Month Number "
- << (*MyIterator).second << endl;
- else
- cout << "Enter a Valid Month (example: March)" << endl;
- }
-
- // empty MyMap - note that clear simply calls erase(begin(),end());
- MyMap.clear();
-
- if (!MyMap.empty())
- cout << "MyMap has " << MyMap.size() << " entries" << endl;
- else
- cout << "MyMap is empty" << endl;
-
- cout << "Entering new entries in MyMap" << endl;
- // Fill MyMap with the days of the week, each mapped to an int
- MyMap["Monday"] = 1;
- MyMap["Tuesday"] = 2;
- MyMap["Wednesday"] = 3;
- MyMap["Thursday"] = 4;
- MyMap["Friday"] = 5;
- MyMap["Saturday"] = 6;
- MyMap["Sunday"] = 7;
-
- if (!MyMap.empty())
- cout << "MyMap has " << MyMap.size() << " entries" << endl;
- else
- cout << "MyMap is empty" << endl;
-
- // Ask the user for a day of the week and print the number
- // that corresponds to the day entered
- MyIterator = MyMap.end();
- while (MyIterator == MyMap.end())
- {
- cout << endl << "Enter a Day of the Week: ";
- cin >> MyBuffer;
- if ((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
- cout << (*MyIterator).first << " is Day Number "
- << (*MyIterator).second << endl;
- else
- cout << "Enter a Valid Day of the Week(example: Monday)" << endl;
- }
-
- // Now clear MyMap again - this time using erase instead of clear
- MyMap.erase(MyMap.begin(), MyMap.end());
-
- if (!MyMap.empty())
- cout << "MyMap has " << MyMap.size() << " entries" << endl;
- else
- cout << "MyMap is empty" << endl;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。