Use of string and vector libraries

More and more use is being made of the standard libraries in C++ programming. The two most important ones are the string and vector template libraries. The string template can replace most of the use of C strings (which use the char* type). The vector template can replace most of the uses of the simple array type in C. The attached assignment and solution shows how these two can be used together in a program.

The string class

By including the header file string, you can use type safe strings of characters in your programs. In fact, the type string is a typedef for basic_string<char>, so basic_string is a a template for using character strings. The important capabilities of the type string are:

string str("abcd")     // makes a string str from the C string "abcd"
str[n]                 // returns a reference to the character at index n in the string str
str.length()           // returns the number of characters in the string 
str = "abcd"           // copies the C string "abcd" into the string str (also works with a string on the RHS)
str.c_str()            // returns the C string from the string str
str.append("abcd")     // appends the C string "abcd" to the string in str
// (also works with an argument of type string) cout << str // << is overloaded for the type string cin >> str // so is >> str1 > str2 // strings can be compared with any relational operator str.find('x') // returns the index of the character x in the string str,
// or the constant string::npos if the character is not found
// (also works with an argument which is a C string or a string)

The vector template

By including the header file vector, you can use encapsulated arrays of any type of element. Since an array is a container, the vector class is a template, i.e. it takes a single parameter which is the name of the type of its elements. For instance: vector<int> is an array of integers, and vector<float*> is an array of pointers to floats. In fact, any type can be used as the parameter to the template. In the attached program, the type used is Book, so vector<Book> is an array of books. The Library program shows how to use a vector as if it was a C array. In fact only the declaration of the type of collection was changed; the rest of the program remained exactly the same. This shows that a vector can be considered to be an array; no more knowledge is necessary to use it. Notice the use of the initializer list in the constructor for Library:

class Library {
private:
  vector<Book> collection;
...
public:
  Library() : collection(100) { number = 0; }
...
}; 

The value 100 is passed to the constructor for the class vector<Book> to make sure 100 elements of type Book are provided in the (internal) array of the object collection.

 

The important capabilities of the vector template class are:

vector<int> arr             // create an empty vector of integers arr
vector<int> arr(10)         // create a vector, arr, of 10 integers
arr.size()                  // returns the number of elements in the vector arr
arr.empty()                 // returns true if the vector arr has no elements, false otherwise
arr[n]                      // returns a reference to the nth element or arr
arr1 = arr2                 // copy vector arr2 into vector arr1 (which is destroyed)
arr.push_back(20)           // add an item dynamically to the end of the vector arr
                            // (the vector is extended, as necessary)
arr.pop_back()              // remove an item from the end of arr
for (int n = 0; n < arr.size(); n++)  // a typical way to process the elements of the vector arr