We learned that we can build a String (Array) class that is better than the C-style,
char * strings (pointer-based arrays) that C++ absorbed from C.
- Classes built into C++
- Available for anyone to use
- string ; Similar to our String class
- vector; Dynamically resizable array
- Redo our String and Array examples
- Class string
- Header string, namespace std
- Can initialize string s1("hi");
- Overloaded ; cout s1
- Overloaded relational operators; == !=
- Assignment operator =
- Concatenation (overloaded +=)
- Substring function substr
- s1.substr(0, 14); ; Starts at location 0, gets 14 characters
- S1.substr(15) ; Substring beginning at location 15
- Overloaded []
- Access one character
- No range checking (if subscript invalid)
- at function
- s1.at(10)
- Character at subscript 10
- Has bounds checking; will end program if invalid (learn more in Chapter 13)
The programs of Figs. 17-19 reimplements the program of Figs. 7-10, using standart class string.
Figure 17:
Standart library class string (part 1 of 2).
|
Figure 18:
Standart library class string (part 2 of 2).
|
Figure 19:
Standart library class string, output.
|
- Class vector
- Header vector, namespace std
- Store any type; vector int myArray(10)
- Function size ( myArray.size() )
- Overloaded []; get specific element, myArray[3]
- Overloaded !=, ==, and =; inequality, equality, assignment
The programs of Figs. 20-23 reimplements the program of Figs. -,
using standart class vector.
Figure 20:
Standart library class vector. (part 1 of 3)
|
Figure 21:
Standart library class vector. (part 2 of 3)
|
Figure 22:
Standart library class vector. (part 3 of 3)
|
Figure 23:
Standart library class vector, output.
|
2004-07-15