190 likes | 388 Views
STL string class. Programming Team 2/15/2006. 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
E N D
STL string class Programming Team 2/15/2006
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
string operators • By default, all comparison operators work • ==, <, <=, !=, >, >= • + operator is concatenation • 4 versions: • string + string • string + c-string • c-string + string • string + char
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
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
string methods • empty() • Returns a boolean indicating whether or not the string is empty • length() • size() • Return the number of characters in the string
string methods • at(int index) • [int index] • Return the character in the string at a given position
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
string methods • substr(int index = 0, int length = npos) • Returns substring of invoker
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)
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)
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)
string methods • erase – Deletes a portion of the string • erase(int index = 0, int length = npos)
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
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
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)
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; }
More information http://www.cprogramming.com/tutorial/string.html