Professional C__ - Marc Gregoire [218]
strings
You can think of a string as a sequential container of characters. Thus, it shouldn’t be surprising to learn that the C++ string is a full-fledged sequential container. It contains begin() and end() methods that return iterators into the string, insert(), push_back() and erase() methods, size(), empty(), and all the rest of the sequential container basics. It resembles a vector quite closely, even providing methods reserve() and capacity(). However, unlike vectors, strings are not required to store their elements contiguously in memory.
The C++ string is actually a typedef of a char instantiation of the basic_string template class. However, we refer to string for simplicity. The discussion here applies equally to wstring and other instantiations of the basic_string template.
You can use string as an STL container just as you would use vector. Here is an example:
string str1;
str1.insert(str1.end(), 'h');
str1.insert(str1.end(), 'e');
str1.insert(str1.end(), 'l');
str1.insert(str1.end(), 'l');
str1.insert(str1.end(), 'o');
for (auto it = str1.cbegin(); it != str1.cend(); ++it) {
cout << *it;
}
cout << endl;
for (auto& letter : str1) {
cout << letter;
}
cout << endl;
Code snippet from StringContainers\StringExample.cpp
In addition to the STL sequential container methods, strings provide a whole host of useful methods and friend functions. The string interface is actually quite a good example of a cluttered interface, one of the design pitfalls discussed in Chapter 4. The string class is discussed in much more detail in Chapter 14, but this section showed you how strings can be used as STL containers.
Streams
Input and output streams are not containers in the traditional sense: They do not store elements. However, they can be considered sequences of elements, and as such share some characteristics with the STL containers. C++ streams do not provide any STL-related methods directly, but the STL supplies special iterators called istream_iterator and ostream_iterator that allow you to “iterate” through input and output streams. Chapter 17 explains how to use them.
bitset
The bitset is a fixed-length abstraction of a sequence of bits. A bit can represent only two values, 1 and 0, which can be referred to as on/off, true/false, etc. The bitset also uses the terminology set and unset. You can toggle or flip a bit from one value to the other.
The bitset is not a true STL container: It’s of fixed size, it’s not templatized on an element type, and it doesn’t support iteration. However, it’s a useful utility, which is often lumped with the containers, so we provide a brief introduction here. The Standard Library Reference resource on the website contains a thorough summary of the bitset operations.
bitset Basics
The bitset, defined in the You can adjust the values of the individual bits with the set(), reset(), and flip() methods, and you can access and set individual fields with an overloaded operator[]. Note that operator[] on a non-const object returns a proxy object to which you can assign a Boolean value, call flip(), or complement with ~. You can also access individual fields with the test() method. Additionally, you can stream bitsets with the normal insertion and extraction operators. The bitset is streamed as a string of 0 and 1 characters. Here is a small example: bitset<10> myBitset; myBitset.set(3); myBitset.set(6); myBitset[8] = true; myBitset[9] = myBitset[3]; if (myBitset.test(3)) { cout << "Bit 3 is set!"<< endl; } cout << myBitset << endl; Code snippet from BitsetBasics\BitsetBasics.cpp The output