string

construct with no arguments, e.g.,   string str1, str2;

member functions include:

int length(); // returns number of chars in string int find(string substring, int startpos = 0); // returns position of first occurrence of // substring, starting at startpos int find(char ch, int startpos = 0); // similarly finds a character string substr(int startpos, int size); // returns substring starting at startpos, // of length size void insert(int startpos, string substring); // inserts substring at startpos position void remove(int startpos, int size); // removes substring starting at startpos, // of length size char * c_str(); // returns equivalent C-style string

operators include:

[] // indexing (note: first char at index 0) + // concatenation = // assignment == != < <= > >= // relational operators

vector

templated, construct with one argument, the vector size e.g.,   vector<int> nums(100);

member functions include:

int length(); // returns size of vector void resize(int newsize); // resizes the vector to newsize, // copying any old entries void push_back(const <TYPE> & item); // adds item to end of vector void pop_back(); // removes item at end of vector <TYPE> & back(); // returns item at end of vector

operators include:

[] // indexing (note: first item at index 0) = // assignment


// Demonstration program to show the use of strings and vectors. // // The program reads in strings and displays statistics, including the // total number of words, total number of unique words, and longest and // shortest word lengths. It terminates on the END-OF-INPUT character // (^Z for Windows, ^D for UNIX). // // Author: Dave Reed /////////////////////////////////////////////////////////////////////// #include <iostream> // NEEDED for cin, cout #include <string> // NEEDED for string #include <vector> // NEEDED for vector using namespace std; int main() { int totalWords = 0; int longest = 0, shortest = 10000; // ASSUMES NO STRING > 10000 CHARS vector<string> unique; // CREATES DEFAULT VECTOR, SIZE 0 string str; // READ STRINGS (DELIMITED BY while (cin >> str) { // WHITESPACE) UNTIL ^Z totalWords++; // UPDATE TOTAL # OF WORDS if (str.length() > longest) { // UPDATE LONGEST WORD IF NEC. longest = str.length(); } if (str.length() < shortest) { // UPDATE SHORTEST WORD IF NEC. shortest = str.length(); } bool found = false; // SEARCH FOR WORD IN for (int i = 0; i < unique.size(); i++) { // VECTOR OF UNIQUE WORDS if (unique[i] == str) { // SO FAR. found = true; // SET FLAG AND EXIT LOOP break; // IF FOUND. } } if (!found) { // IF NOT FOUND, unique.push_back(str); // ADD TO END OF VECTOR } } cout << "Total number of words = " << totalWords << endl; cout << "Number of unique words = " << unique.size() << endl; if (totalWords > 0) { cout << "The shortest word was length " << shortest << endl; cout << "The longest word was length " << longest << endl; } return 0; }


Other useful libraries

fstream          ifstream and ofstream class definitions for reading/writing files

construct with one argument, a C-style string, e.g.,

ifstream instr("foo.in"); cin << filename; ofstream outstr(filename.c_str());

can read from/write to files streams just like standard streams

instr >> x; outstr << "Howdy" << endl;

strstream          istrstream class definition for reading info from strings (useful for processing line-oriented text)

construct with one argument, a C-style string, e.g.,

istrstream instr("a b c");

can read from string streams just like standard streams

while (instr >> letter) { cout << letter << endl; }

cctype          routines for manipulating and testing characters

bool isalpha(ch) returns true if ch is alphanumeric bool isupper(ch) returns true if ch is an upper case letter ('A'..'Z') bool islower(ch) returns true if ch is a lower case letter ('a'..'z') bool isdigit(ch) returns true if ch is a digit ('0'..'9') bool isspace(ch) returns true if ch is whitespace (space, tab, CR) bool isspunct(ch) returns true if ch is a punctuation mark char toupper(ch) returns upper case version of ch (or just ch if not a letter) char tolower(ch) returns lower case version of ch (or just ch if not a letter)

iomanip          routines for manipulating I/O display

setprecision(N) sets flags so that next value displayed (if real) will have N digits to the right of the decimal place setiosflags(ios::fixed) sets flags so that real values are displayed with a fixed number of digits (as set by setprecision)


// Demonstration program to show the use of input string streams to // read and process line-oriented data, and also the use of // setprecision and setiosflags to control the display of real values. // // The program reads in lines of integers and displays the average of // the integers on each line. It terminates when the input line // contains only the word "DONE". // // Author: Dave Reed /////////////////////////////////////////////////////////////////////// #include <iostream> // NEEDED FOR cin #include <iomanip> // NEEDED FOR setprecision #include <string> // NEEDED for string #include <strstream> // NEEDED for istrstream using namespace std; int main() { string str; // CAN READ ENTIRE LINE OF TEXT getline(cin, str); // INTO A STRING (EATS EOL CHAR) while (str != "DONE") { istrstream istr(str.c_str()); // CREATE INPUT STRING STREAM TO // HOLD STRING CONTENTS (NOTE: // MUST CONVERT C++ STRING INTO // C-STYLE STRING) int num, count = 0; double sum = 0; while (istr >> num) { // CAN READ FROM INPUT STRING sum += num; // JUST LIKE ANY OTHER STREAM count++; } // CAN SET THE PRECISION (NUM // DIGITS TO RIGHT OF DECIMAL // PLACE) FOR REAL VALUES if (count == 0) { cout << "There were no numbers to average." << endl; } else { cout << "The average of the " << count << " numbers is " << setprecision(3) << setiosflags(ios::fixed) << sum/count << endl; } getline(cin, str); } return 0; }