Professional Documents
Culture Documents
Object-Orientated Programming For Automation and Robotics: Lecture 5: File IO, Maps, Typedefs
Object-Orientated Programming For Automation and Robotics: Lecture 5: File IO, Maps, Typedefs
Maps
Maps
Maps
Maps
Maps
Overview
Maps
Maps
# include < fstream > # include < iostream > int main () { std :: ifstream in_file ( " input . txt " ) ; if ( not in_file ) std :: perror ( " input . txt " ) ; else { int x (0) ; while ( in_file >> x ) std :: cout << x << " \ n " ; in_file . close () ; } }
(listing 13-1.cpp)
Maps
1 In order to use le IO, one has to include fstream 5 Create a new std::ifstream variable (an object) and ask it to try to open the le input.txt
std::ifstream (= input le stream) is a data-type declared
in fstream
line
6 Check if input.txt was opened correctly. (Similar to checking after reading from std::cin) 7 If not: print an error message. We use std::perror
10 Read integers from in_file as long as possible. 12 Finally, close in_file. This should always be done.
Reading from les works in exactly the same way as reading from std::cin: just remember that you have to open the le rst, and that you should close it later.
Maps
Writing to les
Lets try to nd out what the following program does (leIO.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# include < fstream > int main () { std :: ifstream in_file ( " input . txt " ) ; if ( not in_file ) std :: perror ( " input . txt " ) ; else { std :: ofstream out_file ( " output . txt " ) ; if ( not out_file ) std :: perror ( " output . txt " ) ; else { int x (0) ; int i (1) ; while ( in_file >> x ) out_file < < " line " << i ++ <<" : " <<x < < " \ n " ; out_file . close () ; in_file . close () ; } } }
Maps
6 Create a new std::ofstream variable (an object) and ask it to try to open the le output.txt
std::ofstream (= output le stream) is a data-type
declared in fstream
line line line
12 Write the integers read from in_file (line 11) to out_file. Notice the similarity to writing to std::cout 13 Close out_file. This should always be done.
line
Writing to les works in exactly the same way as writing to std::cout. First open a le, check whether it could be opened, dont forget to close it.
Maps
Characters
The data-type char presents single characters. char literals (constants in program code) have to be enclosed in single quotation marks. Example: c. chars are special integers: A limited form of arithmetic is available: for instance, 7 - 0 == 7 holds. This is not so useful as it used to be (most computers
Maps
Maps
int main () { std :: string monthName ; int year (0) ; int day (0) ; char c ( ) ; std :: cin >> monthName ; std :: cin >> day ; while ( c != \ n and std :: cin . get ( c ) ) if ( c >= 0 and c <= 9 ) { year = c - 0 ; while ( std :: cin . get ( c ) and c >= 0 and c <= 9 ) year = year *10 + c - 0 ; } std :: cout << year < < / << monthName < < / << day ; }
Maps
Overview
Maps
Maps
Maps
A map (= dictionary, association) is a data-structure which
stores pairs of keys and values. For example, the following declares a map of std::strings and ints (one has to use #include <map>):
std :: map < std :: string , int > wordcounts ;
the keys are std::strings and the values are ints. Keys are unique inside a map. The map allows us to access the values through the keys:
++ wordcounts [ " hi " ]; wordcounts["hi"] gives access to the value stored for key "hi". We can use it like any int-variable (increase it, . . . ) When accessing a value for an unknown key, a pair consisting
of the key and a default value is added. This can cause problems.
Maps
not in the map, this key together with a default value is automatically added.
Maps
# include < iostream > # include <map > int main () { std :: map < int , int > histogram ; int x ; while ( std :: cin >> x ) ++ histogram [ x ]; // now we iterate over the histogram and print out all the ( key , value ) - pairs : for ( std :: map < int , int >:: iterator iter ( histogram . begin () ) ; iter != histogram . end () ; ++ iter ) std :: cout << iter - > first << \ t << iter - > second << \ n ; }
11 12 13 14 15 16
Maps
Finding in maps
You can use find(key) to check whether a value with the given key exists in a map.
If a value could be found, find returns an iterator pointing
to this value
otherwise, find returns the end() of the map.
Since the pairs in a map are sorted using the keys, find() is very fast.
Maps
monthName == "february", day == 22, year == 2009 How to translate the monthName to an int? Any ideas?
How about using a std::map<std::string, int> to get the
Maps
monthName == "february", day == 22, year == 2009 How to translate the monthName to an int? Any ideas?
How about using a std::map<std::string, int> to get the
Maps
The following code reads this data into the map mNames:
1 2 3 4 5 6 7 8 9 10 11
std :: map < std :: string , int > mNames ; std :: ifstream namefile ( " monthnames . txt " ) ; if ( not namefile ) std :: perror ( " monthnames . txt " ) ; else { std :: string s ; int i (0) ; while ( namefile >> s >> i ) mNames [ s ] = i ; namefile . close () ; }
Maps
4. calculate the day corresponding to the date as in solution 4.4. Advantage of storing names of the months in a le: we can add new names without recompiling the program. See dayscomplete.cpp.
Maps
Overview
Maps
Maps
Typedefs
The names of the data-types we use in our programs can be quite long (remember std::vector<int>::iterator ?)
C++ allows us to give new names to types using typedef: typedef std :: vector < string >:: iterator string_iter ;
t y p e d e f d a t a t y p e newname ;
typedefs follow the same scope-rules as variable denitions. Choosing good names for data-types makes your programs easier to write/read/understand, and easier to modify.
Maps
Constants
Values that are never going to change can be given a name using the const modier. For instance:
int const daysOfYear (365) ;
Constant must be initialized when they are declared and can never be changed. Thus:
daysOfYear = 300; // error !! // value of daysOfYear cannot be changed
You can declare constants of any type (std::string, bool, char, . . . ). As a rule of thumb: always use constants, never literal values! This makes your program easier to read and modify!
Maps