DOC PREVIEW
UT Dallas CS 4337 - Chapter16-c++ map - example

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

IteratorsExampleUsage exampleUsageIteratorsExampleTutorial on C++ STL Map (ordered associative container) – pages 1-4#include <stdio.h>#include <iostream>#include <string>#include <map>using namespace std;int main(int argc, char *argv[]){ map<string, int> numbers; numbers.insert(pair<string,int>("One",1)); numbers.insert(pair<string,int>("Two",2)); numbers.insert(pair<string,int>("Three",3));numbers["Six"] = 6; // Add new entrynumbers["Seven"] = 7; numbers["Eight"] = 8;numbers["Three"] = 333; // or to update string findKey ("Three");string findValue; map<string, int>::iterator it = numbers.find(findKey); if(it!=numbers.end()) printf("The number for %s is %d\n", findKey.c_str(), (*it).second); else printf("Error, the number for %s is not found\n", findKey.c_str()); cout << "\n"; for(map<string, int>::iterator iter=numbers.begin(); iter != numbers.end(); iter++) { cout << iter->first << " => " << iter->second << '\n'; } cout << "\n"; for(auto iterator = numbers.begin(); iterator != numbers.end(); iterator++){ cout << iterator->first << " => " << iterator->second << '\n'; }}Process started >>>The number for Three is 333Eight => 8One => 1Seven => 7Six => 6Three => 333Two => 2Eight => 8One => 1Seven => 7Six => 6Three => 333Two => 2<<< Process finished.1112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758592Tutorial on C++ STL Map (ordered associative container) – pages 1-4http://en.wikipedia.org/wiki/Associative_containersIn the above example, six elements are entered using the insertion function, and then the first element is deleted. Then, the size of the map is output. Next, the user is prompted for a key to search for. Using the iterator, the find function searches for an element with the given key. If it finds the key, the program prints the element's value. If it does not find it, an iterator to the end ofthe map is returned and it outputs that the key could not be found. Finally all the elements in the tree are erased.IteratorsMaps may use iterators to point to specific elements in the container. An iterator can access both the key and the mapped value of an element:[1]map<Key,T>::iterator it; // declares a map iteratorit->first; // the key value it->second; // the mapped value(*it); // the "element value", which is of type: pair<const Key,T>Below is an example of looping through a map to display all keys and values using iterators:#include <iostream>#include <string>#include <map> int main(){ std::map <std::string, int> data{ { "Bobs score", 10 }, { "Martys score", 15 }, { "Mehmets score", 34 }, { "Rockys score", 22 }, { "Rockys score", 23 } /*overwrites the 22 as keys are identical */ }; // Iterate over the map and print out all key/value pairs. for (const auto& element : data) { std::cout << "Who(key = first): " << element.first; std::cout << " Score(value = second): " << element.second << '\n'; } return 0;}This will output the keys and values of the entire map, sorted by keys.211234567891011121314151617181920212223242526272829303132333435363738392Tutorial on C++ STL Map (ordered associative container) – pages 1-4http://en.wikipedia.org/wiki/Associative_containersThe following example illustrates inserting elements into a map using the insert function and searching for a key using a map iterator and the find function:#include <iostream>#include <map>#include <utility> // make_pair int main(){ typedef std::map<char, int> MapType; MapType my_map; // insert elements using insert function my_map.insert(std::pair<char, int>('a', 1)); my_map.insert(std::pair<char, int>('b', 2)); my_map.insert(std::pair<char, int>('c', 3)); my_map.insert(MapType::value_type('d', 4)); // all standard containers provide this typedef my_map.insert(std::make_pair('e', 5)); // can also use the utility function make_pair my_map.insert({'f', 6}); // using C++11 initializer list // map keys are sorted automatically from lower to higher. // So, my_map.begin() points to the lowest key value not the key // which was inserted first. MapType::iterator iter = my_map.begin(); // erase the first element using the erase function my_map.erase(iter); // output the size of the map std::cout << "Size of my_map: " << my_map.size() << '\n'; std::cout << "Enter a key to search for: "; char c; std::cin >> c; // find will return an iterator to the matching element if it is found // or to the end of the map if the key is not found iter = my_map.find(c); if (iter != my_map.end() ) std::cout << "Value is: " << iter->second << '\n'; else std::cout << "Key is not in my_map" << '\n'; // clear the entries in the map my_map.clear();}311234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950512Tutorial on C++ STL Map (ordered associative container) – pages 1-4http://www.cplusplus.com/reference/map/map/map/Example12345678910111213141516171819202122232425262728293031// constructing maps#include <iostream>#include <map>bool fncomp (char lhs, char rhs) {return lhs<rhs;}struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;}};int main (){ std::map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; std::map<char,int> second (first.begin(),first.end()); std::map<char,int> third (second); std::map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0;}The code does not produce any output, but demonstrates some ways in which a map container can be constructed.4112345672Tutorial on C++ STL Map (ordered associative container) – pages 1-4http://en.wikipedia.org/wiki/Unordered_associative_containers_(C%2B%2B)Usage example#include <iostream>#include <string>#include <unordered_map> int main(){ std::unordered_map<std::string, int> months; months["january"] = 31; months["february"] = 28; months["march"] = 31; months["april"] = 30; months["may"] = 31; months["june"] = 30; months["july"] = 31; months["august"] = 31; months["september"] = 30; months["october"] = 31; months["november"] = 30; months["december"]


View Full Document

UT Dallas CS 4337 - Chapter16-c++ map - example

Download Chapter16-c++ map - example
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter16-c++ map - example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter16-c++ map - example 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?