CSE390C Final Cheat Sheet handout #6 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 Basic Operations of STL structures: function/operator vector list set multiset --------------------------------------------------------- push_back(value) x x - - push_front(value) - x - - insert(value) - - x x back() x x - - front() x x - - pop_back() x x - - pop_front() - x - - x[index] x - - - size() x x x x empty() x x x x clear() x x x x ==, !=, <, >, <=, >= x x x x
Use of iterators STL structures: function/operator vector list set multiset --------------------------------------------------------- begin() x x x x end() x x x x *iterator (const) x x x x *iterator (reference) x x - - iterator+n and +=n x - - - iterator-n and -=n x - - - erase(iterator) x x x x insert(iterator, value) x x x x Basic operations on maps: function/operator ----------------- insert(pair) x[index] (reference to the value associated with this key) size() empty() clear() count(key) (returns a count of the # of keys equal to key, always 0 or 1 Use of iterators on maps: function/operator ---------------------- begin() end() *iterator (const) erase(iterator) insert(iterator, value) pair<type1, type2> first--returns first element in the pair second--returns second element in the pair use type unordered_set for a set of objects that do not overload operator < use type unordered_map for a map whose keys do not overload operator < Examples of available algorithms: find(start, end, value) first occurrence in [start, end) or end if not found count(start, end, value) number of occurrences of value in [start, end) equal(start1, end1, whether values in [start1, end1) are all equal start2, end2) to the values in [start2, end2) replace(start, stop, replaces all occurrences of old with new in old, new) [start, end) reverse(start, stop) reverses the order of the elements [start, end) random_shuffle(start, stop) randomly shuffles values in [start, end) sort(start, end) sorts elements from [start, end) is_sorted(start, stop) returns whether values in range are sorted merge(start1, stop1, merges sorted [start1, stop1) with sorted start2, stop2, start3) [start2, stop2), storing in start3 accumulate(start, stop, s) sum of values in [start, stop), init sum s
Stuart Reges
Last modified: Wed Jun 1 17:45:50 PDT 2022