// compile with g++ maptest.cpp #include #include using namespace std; int main() { map m1; cout << "Assigning values" << endl; m1['j'] = 30; m1['a'] = 40; m1['x'] = 20; m1['k'] = 10; m1['3'] = 80; m1['*'] = 50; cout << "Number of elements in the map: " << m1.size() << endl; cout << "m1['j']: " << m1['j'] << endl; cout << "m1['a']: " << m1['a'] << endl; cout << "m1['x']: " << m1['x'] << endl; cout << "m1['k']: " << m1['k'] << endl; cout << "m1['3']: " << m1['3'] << endl; cout << "m1['*']: " << m1['*'] << endl; cout << "m1['l']: " << m1['l'] << endl; // Value not assigned cout << "Number of elements in the map: " << m1.size() << endl; cout << endl << "Changing values" << endl; m1['j'] = 60; m1['l'] = 70; cout << "m1['j']: " << m1['j'] << endl; cout << "m1['a']: " << m1['a'] << endl; cout << "m1['x']: " << m1['x'] << endl; cout << "m1['k']: " << m1['k'] << endl; cout << "m1['3']: " << m1['3'] << endl; cout << "m1['*']: " << m1['*'] << endl; cout << "m1['l']: " << m1['l'] << endl; cout << endl << "Using an iterator" << endl; map::iterator miter1; for (miter1 = m1.begin(); miter1 != m1.end(); ++miter1) { cout << "Key: " << miter1->first << ", Value: " << miter1->second << endl; } cout << endl << "Which Keys exist in the map (check letters i-n)?" << endl; for (char ch = 'i'; ch <= 'n'; ch++) { cout << " The Key of " << ch ; if (m1.count(ch) == 0) cout << " does not exist in the map." << endl; else cout << " does exist in the map." << endl; } cout << endl << "Delete Key k from the map" << endl; miter1 = m1.find('k'); m1.erase (miter1); cout << "Delete Key l from the map" << endl; m1.erase('l'); cout << endl << "Which Keys exist in the map (check letters i-n)?" << endl; for (char ch = 'i'; ch <= 'n'; ch++) { cout << " The Key of " << ch ; if (m1.count(ch) == 0) cout << " does not exist in the map." << endl; else cout << " does exist in the map." << endl; } cout << endl << "Current keys and values" << endl; for (miter1 = m1.begin(); miter1 != m1.end(); ++miter1) { cout << "Key: " << miter1->first << ", Value: " << miter1->second << endl; } }