STL string class - PowerPoint PPT Presentation

simon-tyson
stl string class n.
Skip this Video
Loading SlideShow in 5 Seconds..
STL string class PowerPoint Presentation
Download Presentation
STL string class

play fullscreen
1 / 19
Download Presentation
STL string class
174 Views
Download Presentation

STL string class

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. STL string class Programming Team 2/15/2006

  2. string constructors • string() = empty string • string(int l, char ch) = string of length l, of repeated ch • string(char* c-string) = string from c-string • string(char* c-string, int l) = string from c-string, starts at 0, goes l characters long (substring basically) • string(string oldString, int start = 0, int length = npos) = copy constructor, allows substring

  3. string operators • By default, all comparison operators work • ==, <, <=, !=, >, >= • + operator is concatenation • 4 versions: • string + string • string + c-string • c-string + string • string + char

  4. string constants • .npos => length of longest possible string, usually -1 • Useful when doing find operations (is this substring in the string?) They will usually return npos if not in there

  5. string methods • c_str() • Returns a pointer to a c-style string • copy(char* array, int length, int index=0) • Beginning at index, copy length number of chars from string into char array

  6. string methods • empty() • Returns a boolean indicating whether or not the string is empty • length() • size() • Return the number of characters in the string

  7. string methods • at(int index) • [int index] • Return the character in the string at a given position

  8. string methods • compare(string anotherString) • Returns an integer, less than zero if calling string < anotherString, zero if equal, > 0 if calling string > anotherString • <,> are defined in dictionary order (> farther back in dictionary) • Can also call • compare(char*) • compare(int index, int length, string str) – compare whole str to substring of invoker • compare(int index, int length, string str, int index2, int length2) – compare substring of str to substring of invoker • compare(int index, int length, char* str, int length2 = npos) – compare substring of char array to substring of invoker

  9. string methods • substr(int index = 0, int length = npos) • Returns substring of invoker

  10. string methods • append – Appends another string onto the back of the string • Multiple versions append(string str) append(string str, int index, int length) append(char* array) append(char* array, int length) append(int length, char aChar)

  11. string methods • insert – Inserts another string into the middle of the invoker • Multiple versions insert(int index, string str) insert(int index, string str, int index, int length) insert(int index, char* array) insert(int index, char* array, int length) insert(int index, int length, char aChar)

  12. string methods • Replace - Replaces another string into the middle of the invoker • Multiple versions replace(int index, int length, string str) replace(int index, int length, string str, int index2, int length2) replace(int index, int length, char* array) replace(int index, int length, char* array, int length) replace(int index, int length, int length2, char aChar)

  13. string methods • erase – Deletes a portion of the string • erase(int index = 0, int length = npos)

  14. string methods • int find(str anotherString) – returns the index of the first occurrence in the invoker of anotherString • Versions to look for char*, to start search at different index than 0, to search for substrings of anotherString, to search for a char only • int find_first_of(str charsToLookFor) – returns the index of the first occurrence in the invoker of any character in the string charsToLookFor • Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only • find_first_not_of - returns the index of the first occurrence in the invoker of any character not in the string charsToLookFor • Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only

  15. string methods • rfind - returns the index of the last occurrence in the invoker of anotherString • Versions to look for char*, to start search at different index than 0, to search for substrings of anotherString, to search for a char only • int find_last_of(str charsToLookFor) – returns the index of the first occurrence in the invoker of any character in the string charsToLookFor • Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only • find_last_not_of - returns the index of the first occurrence in the invoker of any character not in the string charsToLookFor • Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only

  16. string methods • Iterators – A way of traversing through a datastructure, an element at a time • For strings, this is a char at a time • begin() – Return an interator to the front of the string • end() – Return an iterator to the very back of the string (past the end – usually used for checking if iterated far enough)

  17. Iterators string aString = "helloworld"; string::iterator it = aString.begin(); while (it != aString.end()) { cout << "Iterator: " << *it << endl; it++; } cout << endl; for (int i = 0; i < aString.length(); i++) { cout << "For: " << aString[i] << endl; }

  18. More information http://www.cprogramming.com/tutorial/string.html

  19. Practice Problems