#include #include #include using namespace std; /* A sample program to illustrate vectors and strings. @author - Sami Rollins */ void main() { //declare a vector of 10 strings vector names(10); //set elements of the vector //as you would set elements of an array names[0] = "Ophelia"; names[1] = "Cymbeline"; names[2] = "Helena"; names[3] = "Juliet"; names[4] = "Lady Macbeth"; names[5] = "Viola"; names[6] = "Portia"; names[7] = "Puck"; names[8] = "Miranda"; names[9] = "Lavinia"; //access and print each element of the vector for(int i = 0; i < names.size(); i++) { cout << "Names " << i << ": " << names.at(i) << endl; } //access and print each element of the vector using //array syntax for(int j = 0; j < names.size(); j++) { cout << "Names " << j << ": " << names[j] << endl; } //access first/last element of the vector using member functions cout << "The first element is: " << names.front() << endl; cout << "The last element is: " << names.back() << endl; //manipulate strings using string member functions cout << "The length of the name at position 3 is " << names.at(3).length() << endl; cout << "The name at position 7 contains 'uck' starting at position " << names[7].find("uck") << endl; cout << "The name at position 6 does not contain 'uck' " << names.at(6).find("uck") << endl; //access individual characters of strings using c-style strings bool contains_space = false; const char* c_str_name4 = names[4].c_str(); for(int k = 0; k < names[4].length(); k++) { if(c_str_name4[k] == ' ') { contains_space = true; } } if(contains_space) { cout << "The name at position 4 contains a space." << endl; } else { cout << "The name at position 4 does not contain a space." << endl; } contains_space = false; const char* c_str_name9 = names[9].c_str(); for(int l = 0; l < names[9].length(); l++) { if(c_str_name9[l] == ' ') { contains_space = true; } } if(contains_space) { cout << "The name at position 9 contains a space." << endl; } else { cout << "The name at position 9 does not contain a space." << endl; } } /* Program Output Below */ /* Names 0: Ophelia Names 1: Cymbeline Names 2: Helena Names 3: Juliet Names 4: Lady Macbeth Names 5: Viola Names 6: Portia Names 7: Puck Names 8: Miranda Names 9: Lavinia Names 0: Ophelia Names 1: Cymbeline Names 2: Helena Names 3: Juliet Names 4: Lady Macbeth Names 5: Viola Names 6: Portia Names 7: Puck Names 8: Miranda Names 9: Lavinia The first element is: Ophelia The last element is: Lavinia The length of the name at position 3 is 6 The name at position 7 contains 'uck' starting at position 1 The name at position 6 does not contain 'uck' 4294967295 The name at position 4 contains a space. The name at position 9 does not contain a space. Press any key to continue */