COP 3530 Data Structures & Algorithms Discussion Session 3
Outline Input-output streams in C++ Floating point precision File manipulation Pointers in C Vector class in C++ Strings in C and C++
About me Eyup Serdar Ayaz TA Office: E309 This week no office hours
Input and output streams in C++ Stream classes Stream objects Insertion ( >) operators
Floating point precision default precision scientific precision fixed precision
File manipulation in C++ Include necessary header: fstream Declare an file stream: ifstream or ofstream Open the stream file Check if the file is opened Read from or write to the stream Close the stream
File manipulation example
Pointers in C Reference operator (&) Dereference operator (*) Types of pointers Arrays as pointers Pointer arithmetic Pointers to pointers Null pointer
Vector class in C++ An STL (Standard Template Library) container An strict sequence container i.e. An array of variable size. Similar to ArrayList of Java Defined as a template, type should be specified Most useful functions: operator[] size insert erase begin end
A vector example
Strings in C Strings are char arrays Declared as char arrays or pointers Ends with ‘\0’ or simply 0 String manipulation functions are defined in Cannot be assigned directly other than declaration Frequently used functions: strcpy strcmp strcat strlen
cstring example
String class in C++ Strings are objects String manipulation functions are defined in Can be directly assigned Can be concatenated using + operator Frequently used functions: operator[] or at size copy compare substr operator+= or append
C++ String example(Comparison)
References cpp/io/readtextfile.html cpp/io/readtextfile.html s/setprecision/ s/setprecision/