CSE390C Midterm Cheat Sheet handout #2 some common string operations: output << str; insert str onto output stream input >> str; extract str from input stream (one token) getline(input, str) read into str one line from input str.empty() is str empty? str.size() how many characters in str? str[index] reference to character at given index str1 + str2 result of adding two strings (a new string) ==, !=, <, >, <=, >= relational operators to compare two strings istringstream input(str) make an input stream from the given string str.substr(index, n) string of n characters starting at index str.substr(index) string of characters starting at index str.find(str2) first occurrence of str2 in str or -1 str.rfind(str2) last occurrence of str2 in str or -1 some common vector operations: v.push_back(n) append n to end of vector v.empty() is v empty? v.size() how many elements in v? v[index] reference to value at given index ==, != compares all values for equality some common stream operations: ifstream input(name); initializes ifstream to read from given file ofstream output(name); initializes ofstream to write to given file stream >> x; read (extract) x from given input stream stream << x; write (insert) x to given output stream getline(input, str) read into str one line from input stream.open(name); opens the stream to the given file stream.close() closes the stream istringstream input(str) make an input stream from the given string ostringstream output() make an output stream that will go to a string stream.str() convert an ostringstream to a string some common math functions: abs(x) returns the absolute value of x max(x, y) returns the larger of x and y min(x, y) returns the smaller of x and y pow(x, y) returns the value of x to the power of y round(n) rounds to the nearest integer sqrt(n) returns the square root of n
Stuart Reges
Last modified: Fri Feb 3 10:24:33 PST 2023