UT Dallas CS 4337 - Chapter16-c++ map - example (10 pages)

Previewing pages 1, 2, 3 of 10 page document View the full content.
View Full Document

Chapter16-c++ map - example



Previewing pages 1, 2, 3 of actual document.

View the full content.
View Full Document
View Full Document

Chapter16-c++ map - example

28 views


Pages:
10
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

1Tutorial on C STL Map ordered associative container pages 1 4 1 include stdio h 2 include iostream 3 include string 4 include map 5using namespace std 6 7int main int argc char argv 8 9 map string int numbers 10 11 numbers insert pair string int One 1 12 numbers insert pair string int Two 2 13 numbers insert pair string int Three 3 14 15 numbers Six 6 Add new entry 16 numbers Seven 7 17 numbers Eight 8 18 19 numbers Three 333 or to update 20 21 string findKey Three 22 string findValue 23 24 map string int iterator it numbers find findKey 25 26 if it numbers end 27 printf The number for s is d n findKey c str it second 28 else 29 printf Error the number for s is not found n findKey c str 30 cout n 31 32 33 for map string int iterator iter numbers begin 34 iter numbers end iter 35 cout iter first iter second n 36 37 cout n 38 39 for auto iterator numbers begin iterator numbers end iterator 40 cout iterator first iterator second n 41 42 43Process started 44The number for Three is 333 45 46Eight 8 47One 1 48Seven 7 49Six 6 50Three 333 51Two 2 52 53Eight 8 54One 1 55Seven 7 56Six 6 57Three 333 58Two 2 59 Process finished 2 1 1Tutorial on C STL Map ordered associative container pages 1 4 1http en wikipedia org wiki Associative containers 2In the above example six elements are entered using the insertion function and then the first 3element is deleted Then the size of the map is output Next the user is prompted for a key to 4search for Using the iterator the find function searches for an element with the given key If it 5finds the key the program prints the element s value If it does not find it an iterator to the end of 6the map is returned and it outputs that the key could not be found Finally all the elements in the 7tree are erased 8Iterators 9Maps may use iterators to point to specific elements in the container An iterator can access both 10the key and the mapped value of an element 1 11map Key T iterator it declares a map iterator 12it first the key value 13it second the mapped value 14 it the element value which is of type pair const Key T 15Below is an example of looping through a map to display all keys and values using iterators 16 include iostream 17 include string 18 include map 19 20int main 21 22 std map std string int data 23 Bobs score 10 24 Martys score 15 25 Mehmets score 34 26 Rockys score 22 27 Rockys score 23 overwrites the 22 as keys are identical 28 29 30 Iterate over the map and print out all key value pairs 31 for const auto element data 32 33 std cout Who key first element first 34 std cout Score value second element second n 35 36 37 return 0 38 39This will output the keys and values of the entire map sorted by keys 2 2 1Tutorial on C STL Map ordered associative container pages 1 4 1http en wikipedia org wiki Associative containers 2The following example illustrates inserting elements into a map using the insert function and 3searching for a key using a map iterator and the find function 4 include iostream 5 include map 6 include utility make pair 7 8int main 9 10 typedef std map char int MapType 11 MapType my map 12 13 insert elements using insert function 14 my map insert std pair char int a 1 15 my map insert std pair char int b 2 16 my map insert std pair char int c 3 17 my map insert MapType value type d 4 18 all standard containers provide this typedef 19 my map insert std make pair e 5 20 can also use the utility function make pair 21 my map insert f 6 using C 11 initializer list 22 23 map keys are sorted automatically from lower to higher 24 So my map begin points to the lowest key value not the key 25 which was inserted first 26 27 MapType iterator iter my map begin 28 29 erase the first element using the erase function 30 my map erase iter 31 32 output the size of the map 33 std cout Size of my map my map size n 34 35 std cout Enter a key to search for 36 char c 37 std cin c 38 39 find will return an iterator to the matching element if it is found 40 or to the end of the map if the key is not found 41 42 iter my map find c 43 if iter my map end 44 std cout Value is iter second n 45 else 46 std cout Key is not in my map n 47 48 clear the entries in the map 49 my map clear 50 51 2 3 1Tutorial on C STL Map ordered associative container pages 1 4 1http www cplusplus com reference map map map 2Example 1 constructing maps 2 include iostream 3 include map 4 5 bool fncomp char lhs char rhs return lhs rhs 6 7 struct classcomp 8 bool operator const char lhs const char rhs const 9 return lhs rhs 10 11 12 int main 13 14 std map char int first 15 16 first a 10 17 first b 30 18 first c 50 19 first d 70 20 21 std map char int second first begin first end 22 23 std map char int third second 24 25 std map char int classcomp fourth class as Compare 26 27 bool fn pt char char fncomp 28 std map char int bool char char fifth fn pt 29 function pointer as Compare 30 31 return 0 3 4 5The code does not produce any output but demonstrates some ways in which a map container can be 6constructed 7 2 4 1Tutorial on C STL Map ordered associative container pages 1 4 1http en wikipedia org wiki Unordered associative containers C 2B 2B 2Usage example 3 include iostream 4 include string 5 include unordered map 6 7int main 8 9 std unordered map std string 10 11 months january 31 12 months february 28 13 months march 31 14 months april 30 15 months may 31 16 months june 30 17 months july 31 18 months august 31 19 months september 30 20 months october 31 21 months november 30 22 months december 31 23 24 std cout september 25 std cout april 26 std cout december 27 std cout february 28 29 return 0 30 31 2 int months months september months april months december months february 5 std endl std endl std endl std endl 1Tutorial on C STL Map ordered associative container pages 1 4 1http en wikipedia org wiki Associative containers 2Usage 3The following code demonstrates how to use the map string int to count occurrences of 4words It uses the word as the key and …


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

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 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?