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. 3.28-3.30 reimplements the program of Figs. 3.18-3.21, using standart class string.
Figure 3.28:
Standart library class string (part 1 of 2).
|
Figure 3.29:
Standart library class string (part 2 of 2).
|
Figure 3.30:
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. 3.31-3.34 reimplements the program of Figs. 3.8-3.11,
using standart class vector.
Figure 3.31:
Standart library class vector. (part 1 of 3)
|
Figure 3.32:
Standart library class vector. (part 2 of 3)
|
Figure 3.33:
Standart library class vector. (part 3 of 3)
|
Figure 3.34:
Standart library class vector, output.
|
2004-07-29