Professional C__ - Marc Gregoire [312]
} else {
return 1;
}
}
Code snippet from Hashmap\FinalHashmap\hashmap.cpp
The final method, operator[], is not required by the standard, but is provided for convenience of the programmer, and to be symmetric with std::map. The prototype and implementations are identical to those of the operator[] in the STL map. The comments explain the potentially confusing one-line implementation:
template T& hashmap { // This definition is the same as that used by map, according to // the standard. // It's a bit cryptic, but it basically attempts to insert // a new key/value pair of x and a new value. Regardless of whether // the insert succeeds or fails, insert() returns a pair of an // iterator/bool. The iterator refers to a key/value pair, the // second element of which is the value we want to return. return ((insert(make_pair(x, T()))).first)->second; } Code snippet from Hashmap\FinalHashmap\hashmap.cpp Note on Sequential Containers The hashmap developed in the preceding sections is an associative container. However, you could also write a sequential container, or an unordered associative container, in which case you would need to follow a different set of requirements. Instead of listing them here, it’s easier to point out that the deque container follows the prescribed sequential container requirements almost exactly. The only difference is that it provides an extra resize() method (not required by the standard). An example of an unordered associative container is the unordered_map, on which you can model your own unordered associative containers. SUMMARY In the process of reading this chapter, you also hopefully gained an appreciation for the steps involved in developing containers. Even if you never write another STL algorithm or container, you understand better the STL’s mentality and capabilities, and you can put it to better use. This chapter concludes the tour of the STL, Chapters 11 to 17. Even with all the details given in this book, there are still features omitted. If this material excited you, consult some of the resources in Appendix B for more information. On the other hand, we realize that the syntax and material in these chapters was dense. Don’t feel compelled to use all the features discussed here. Forcing them into your programs without a true need will just complicate your code. However, we encourage you to consider incorporating aspects of the STL into your programs where they make sense. Start with the containers, maybe throw in an algorithm or two, and before you know it, you’ll be a convert! PART III Mastering Advanced Features of C++ CHAPTER 18: Overloading C++ Operators CHAPTER 19: Writing Generic Code with Templates CHAPTER 20: Advanced Templates CHAPTER 21: Effective Memory Management CHAPTER 22: Multithreaded Programming with C++ Chapter 18 Overloading C++ Operators What operator overloading is Rationale for overloading operators Limitations, caveats, and choices in operator overloading Summary of operators you can, cannot, and should not overload How to overload unary plus, unary minus, increment, and decrement How to overload the I/O streams operators (operator<< and operator>>) How to overload the subscripting (array index) operator How to overload the function call operator How to overload the dereferencing operators (* and ->) How to write conversion operators How to overload the memory allocation and deallocation operators C++ allows you to redefine the meanings
The final example in this chapter showed almost the complete development of a hashmap associative container and its iterator. This hashmap implementation was given here to teach you how to write your own STL containers and iterators. C++11 includes its own set of unordered associative containers or hash tables. If your compiler supports those standard C++11 hash tables, you should use them instead of your own implementation.
WHAT’S IN THIS CHAPTER?