Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 52 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
52
Dung lượng
120,58 KB
Nội dung
313 z 516 int sz = 1000; if(argc >= 2) count = atoi(argv[1]); if(argc >= 3) sz = atoi(argv[2]); vector<int> vi(sz); clock_t ticks = clock(); for(int i1 = 0; i1 < count; i1++) for(int j = 0; j < sz; j++) vi[j]; cout << "vector[] " << clock() - ticks << endl; ticks = clock(); for(int i2 = 0; i2 < count; i2++) for(int j = 0; j < sz; j++) vi.at(j); cout << "vector::at() " << clock()-ticks <<endl; deque<int> di(sz); ticks = clock(); for(int i3 = 0; i3 < count; i3++) for(int j = 0; j < sz; j++) di[j]; cout << "deque[] " << clock() - ticks << endl; ticks = clock(); for(int i4 = 0; i4 < count; i4++) for(int j = 0; j < sz; j++) di.at(j); cout << "deque::at() " << clock()-ticks <<endl; // Demonstrate at() when you go out of bounds: try { di.at(vi.size() + 1); } catch( ) { cerr << "Exception thrown" << endl; } } ///:~ As you saw in Chapter 1, different systems may handle the uncaught exception in different ways, but you’ll know one way or another that something went wrong with the program when using at ( ), whereas it’s possible to go blundering ahead using operator[ ]. list A list is implemented as a doubly linked list data structure and is thus designed for rapid insertion and removal of elements anywhere in the sequence, whereas for vector and deque this is a much more costly operation. A list is so slow when randomly accessing elements that it does not have an operator[ ]. It’s best used when you’re traversing a sequence, in order, from beginning to end (or vice-versa), rather than choosing elements randomly from the middle. Even then the traversal can be slower than with a vector, but if you aren’t doing a lot of traversals, that won’t be your bottleneck. Another thing to be aware of with a list is the memory overhead of each link, which requires a forward and backward pointer on top of the storage for the actual object. Thus, a list is a better choice when you have larger objects that you’ll be inserting and removing from the middle of the list. It’s better not to use a list if you think you might be traversing it a lot, looking for objects, since the amount of time it takes to get from the beginning of the list—which is the only place you can start unless you’ve already got an iterator to somewhere you know is closer to your destination—to the object of interest is proportional to the number of objects between the beginning and that object. The objects in a list never move after they are created; “moving” a list element means changing 314 z 516 the links, but never copying or assigning the actual objects. This means that iterators aren't invalidated when items are added to the list as it was demonstrated earlier to be the case vector. Here’s an example using the Noisy class: //: C07:ListStability.cpp // Things don't move around in lists //{-bor} #include "Noisy.h" #include <algorithm> #include <iostream> #include <iterator> #include <list> using namespace std; int main() { list<Noisy> l; ostream_iterator<Noisy> out(cout, " "); generate_n(back_inserter(l), 25, NoisyGen()); cout << "\n Printing the list:" << endl; copy(l.begin(), l.end(), out); cout << "\n Reversing the list:" << endl; l.reverse(); copy(l.begin(), l.end(), out); cout << "\n Sorting the list:" << endl; l.sort(); copy(l.begin(), l.end(), out); cout << "\n Swapping two elements:" << endl; list<Noisy>::iterator it1, it2; it1 = it2 = l.begin(); it2++; swap(*it1, *it2); cout << endl; copy(l.begin(), l.end(), out); cout << "\n Using generic reverse(): " << endl; reverse(l.begin(), l.end()); cout << endl; copy(l.begin(), l.end(), out); cout << "\n Cleanup" << endl; } ///:~ Operations as seemingly radical as reversing and sorting the list require no copying of objects, because instead of moving the objects, the links are simply changed. However, notice that sort( ) and reverse( ) are member functions of list, so they have special knowledge of the internals of list and can rearrange the elements instead of copying them. On the other hand, the swap( ) function is a generic algorithm and doesn’t know about list in particular, so it uses the copying approach for swapping two elements. In general, use the member version of an algorithm if that is supplied instead of its generic algorithm equivalent. In particular, use the generic sort( ) and reverse( ) algorithms only with arrays, vectors, and deques. If you have large, complex objects, you might want to choose a list first, especially if construction, destruction, copy-construction, and assignment are expensive and if you are doing things like sorting the objects or otherwise reordering them a lot. Special list operations The list has some special built-in operations to make the best use of the structure of the list. You’ve already seen reverse( ) and sort( ), and here are some of the others in use: //: C07:ListSpecialFunctions.cpp #include <algorithm> 315 z 516 #include <iostream> #include <iterator> #include <list> #include "Noisy.h" using namespace std; ostream_iterator<Noisy> out(cout, " "); void print(list<Noisy>& ln, char* comment = "") { cout << "\n" << comment << ":\n"; copy(ln.begin(), ln.end(), out); cout << endl; } int main() { typedef list<Noisy> LN; LN l1, l2, l3, l4; generate_n(back_inserter(l1), 6, NoisyGen()); generate_n(back_inserter(l2), 6, NoisyGen()); generate_n(back_inserter(l3), 6, NoisyGen()); generate_n(back_inserter(l4), 6, NoisyGen()); print(l1, "l1"); print(l2, "l2"); print(l3, "l3"); print(l4, "l4"); LN::iterator it1 = l1.begin(); it1++; it1++; it1++; l1.splice(it1, l2); print(l1, "l1 after splice(it1, l2)"); print(l2, "l2 after splice(it1, l2)"); LN::iterator it2 = l3.begin(); it2++; it2++; it2++; l1.splice(it1, l3, it2); print(l1, "l1 after splice(it1, l3, it2)"); LN::iterator it3 = l4.begin(), it4 = l4.end(); it3++; it4 ; l1.splice(it1, l4, it3, it4); print(l1, "l1 after splice(it1,l4,it3,it4)"); Noisy n; LN l5(3, n); generate_n(back_inserter(l5), 4, NoisyGen()); l5.push_back(n); print(l5, "l5 before remove()"); l5.remove(l5.front()); print(l5, "l5 after remove()"); l1.sort(); l5.sort(); l5.merge(l1); print(l5, "l5 after l5.merge(l1)"); cout << "\n Cleanup" << endl; } ///:~ The print( ) function displays results. After filling four lists with Noisy objects, one list is spliced into another in three ways. In the first, the entire list l2 is spliced into l1 at the iterator it1. Notice that after the splice, l2 is empty—splicing means removing the elements from the source list. The second splice inserts elements from l3 starting at it2 into l1 starting at it1. The third splice starts at it1 and uses elements from l4 starting at it3 and ending at it4 (the seemingly redundant mention of the source list is because the elements must be erased from the source list as part of the transfer to the destination list). The output from the code that demonstrates remove( ) shows that the list does not have to be sorted in order for all the elements of a particular value to be removed. Finally, if you merge( ) one list with another, the merge only works sensibly if the lists have been 316 z 516 sorted. What you end up with in that case is a sorted list containing all the elements from both lists (the source list is erased—that is, the elements are moved to the destination list). A unique( ) member function removes all duplicates, but only if you sort the list first: //: C07:UniqueList.cpp // Testing list's unique() function #include <iostream> #include <iterator> #include <list> using namespace std; int a[] = { 1, 3, 1, 4, 1, 5, 1, 6, 1 }; const int asz = sizeof a / sizeof *a; int main() { // For output: ostream_iterator<int> out(cout, " "); list<int> li(a, a + asz); li.unique(); // Oops! No duplicates removed: copy(li.begin(), li.end(), out); cout << endl; // Must sort it first: li.sort(); copy(li.begin(), li.end(), out); cout << endl; // Now unique() will have an effect: li.unique(); copy(li.begin(), li.end(), out); cout << endl; } ///:~ The list constructor used here takes the starting and past-the-end iterator from another container and copies all the elements from that container into itself. (A similar constructor is available for all the containers.) Here, the “container” is just an array, and the “iterators” are pointers into that array, but because of the design of the STL, it works with arrays just as easily as any other container. The unique( ) function will remove only adjacent duplicate elements, and thus sorting is necessary before calling unique( ). Four additional list member functions are not demonstrated here: a remove_if( ) that takes a predicate, which decides whether an object should be removed; a unique( ) that takes a binary predicate to perform uniqueness comparisons; a merge( ) that takes an additional argument which performs comparisons; and a sort( ) that takes a comparator (to provide a comparison or override the existing one). list vs. set Looking at the previous example, you might note that if you want a sorted list with no duplicates, a set can give you that, right? It’s interesting to compare the performance of the two containers: //: C07:ListVsSet.cpp // Comparing list and set performance #include <algorithm> #include <iostream> #include <list> #include <set> #include <cstdlib> 317 z 516 #include <ctime> using namespace std; class Obj { int a[20]; // To take up extra space int val; public: Obj() : val(rand() % 500) {} friend bool operator<(const Obj& a, const Obj& b) { return a.val < b.val; } friend bool operator==(const Obj& a, const Obj& b) { return a.val == b.val; } friend ostream& operator<<(ostream& os, const Obj& a) { return os << a.val; } }; template<class Container> void print(Container& c) { typename Container::iterator it; for(it = c.begin(); it != c.end(); it++) cout << *it << " "; cout << endl; } struct ObjGen { Obj operator()() { return Obj(); } }; int main() { const int sz = 5000; srand(time(0)); list<Obj> lo; clock_t ticks = clock(); generate_n(back_inserter(lo), sz, ObjGen()); lo.sort(); lo.unique(); cout << "list:" << clock() - ticks << endl; set<Obj> so; ticks = clock(); generate_n(inserter(so, so.begin()), sz, ObjGen()); cout << "set:" << clock() - ticks << endl; print(lo); print(so); } ///:~ When you run the program, you should discover that set is much faster than list. This is reassuring—after all, it is set’s primary job description! Comment Swapping basic sequences We mentioned earlier that all basic sequences have a member function swap( ) that’s designed to switch one sequence with another (but only for sequences of the same type). The member swap ( ) makes use of its knowledge of the internal structure of the particular container in order to be 318 z 516 efficient: //: C07:Swapping.cpp //{-bor} // All basic sequence containers can be swapped #include "Noisy.h" #include <algorithm> #include <deque> #include <iostream> #include <iterator> #include <list> #include <vector> using namespace std; ostream_iterator<Noisy> out(cout, " "); template<class Cont> void print(Cont& c, char* comment = "") { cout << "\n" << comment << ": "; copy(c.begin(), c.end(), out); cout << endl; } template<class Cont> void testSwap(char* cname) { Cont c1, c2; generate_n(back_inserter(c1), 10, NoisyGen()); generate_n(back_inserter(c2), 5, NoisyGen()); cout << "\n" << cname << ":" << endl; print(c1, "c1"); print(c2, "c2"); cout << "\n Swapping the " << cname << ":" << endl; c1.swap(c2); print(c1, "c1"); print(c2, "c2"); } int main() { testSwap<vector<Noisy> >("vector"); testSwap<deque<Noisy> >("deque"); testSwap<list<Noisy> >("list"); } ///:~ When you run this, you’ll discover that each type of sequence container can swap one sequence for another without any copying or assignments, even if the sequences are of different sizes. In effect, you’re completely swapping the resources of one object for another. The STL algorithms also contain a swap( ), and when this function is applied to two containers of the same type, it uses the member swap( ) to achieve fast performance. Consequently, if you apply the sort( ) algorithm to a container of containers, you will find that the performance is very fast—it turns out that fast sorting of a container of containers was a design goal of the STL. set The set produces a container that will accept only one of each thing you place in it; it also sorts the elements. (Sorting isn’t intrinsic to the conceptual definition of a set, but the STL set stores its elements in a balanced tree data structure to provide rapid lookups, thus producing sorted results when you traverse it.) The first two examples in this chapter used sets. Consider the problem of creating an index for a book. You might like to start with all the words in the book, but you only want one instance of each word, and you want them sorted. Of course, a 319 z 516 set is perfect for this and solves the problem effortlessly. However, there’s also the problem of punctuation and any other nonalpha characters, which must be stripped off to generate proper words. One solution to this problem is to use the Standard C library functions isalpha( ) and isspace( ) to extract only the characters you want. You can replace all unwanted characters with spaces so that you can easily extract valid words from each line you read: //: C07:WordList.cpp // Display a list of words used in a document #include <algorithm> #include <cctype> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <set> #include <sstream> #include <string> #include " /require.h" using namespace std; char replaceJunk(char c) { // Only keep alphas, space (as a delimiter), and ' return (isalpha(c) || c == '\'') ? c : ' '; } int main(int argc, char* argv[]) { char* fname = "WordList.cpp"; if(argc > 1) fname = argv[1]; ifstream in(fname); assure(in, fname); set<string> wordlist; string line; while(getline(in, line)) { transform(line.begin(), line.end(), line.begin(), replaceJunk); istringstream is(line); string word; while (is >> word) wordlist.insert(word); } // Output results: copy(wordlist.begin(), wordlist.end(), ostream_iterator<string>(cout, "\n")); } ///:~ The call to transform( ) replaces each character to be ignored with a space. The set container not only ignores duplicate words, but compares the words it keeps according to the function object less<string> (the default second template argument for the set container), which in turn uses string::operator<( ), so the words emerge in alphabetical order. Comment You don’t have to use a set just to get a sorted sequence. You can use the sort( ) function (along with a multitude of other functions in the STL) on different STL containers. However, it’s likely that set will be faster. Using a set is particularly handy when you just want to do lookup, since its find( ) member function has logarithmic complexity and therefore is much faster than the generic find( ) algorithm. Comment The following version shows how to build the list of words with an istreambuf_iterator that moves the characters from one place (the input stream) to another (a string object), depending on whether the Standard C library function isalpha( ) returns true: //: C07:WordList2.cpp 320 z 516 // Illustrates istreambuf_iterator and insert iterators #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <set> #include <string> #include " /require.h" using namespace std; int main(int argc, char* argv[]) { char* fname = "WordList2.cpp"; if(argc > 1) fname = argv[1]; ifstream in(fname); assure(in, fname); istreambuf_iterator<char> p(in), end; set<string> wordlist; while (p != end) { string word; insert_iterator<string> ii(word, word.begin()); // Find the first alpha character: while(!isalpha(*p) && p != end) p++; // Copy until the first non-alpha character: while (isalpha(*p) && p != end) *ii++ = *p++; if (word.size() != 0) wordlist.insert(word); } // Output results: copy(wordlist.begin(), wordlist.end(), ostream_iterator<string>(cout, "\n")); } ///:~ This example was suggested by Nathan Myers, who invented the istreambuf_iterator and its relatives. This iterator extracts information character by character from a stream. Although the istreambuf_iterator template argument might suggest that you could extract, for example, ints instead of char, that’s not the case. The argument must be of some character type—a regular char or a wide character. After the file is open, an istreambuf_iterator called p is attached to the istream so characters can be extracted from it. The set<string> called wordlist will hold the resulting words. The while loop reads words until the end of the input stream is found. This is detected using the default constructor for istreambuf_iterator, which produces the past-the-end iterator object end. Thus, if you want to test to make sure you’re not at the end of the stream, you simply say p ! = end. The second type of iterator that’s used here is the insert_iterator, which creates an iterator that knows how to insert objects into a container. Here, the “container” is the string called word, which, for the purposes of insert_iterator, behaves like a container. The constructor for insert_iterator requires the container and an iterator indicating where it should start inserting the characters. You could also use a back_insert_iterator, which requires that the container have a push_back( ) (string does). After the while loop sets everything up, it begins by looking for the first alpha character, incrementing start until that character is found. It then copies characters from one iterator to the 321 z 516 other, stopping when a nonalpha character is found. Each word, assuming it is nonempty, is added to wordlist. A completely reusable tokenizer The word list examples use different approaches to extract tokens from a stream, neither of which is very flexible. Since the STL containers and algorithms all revolve around iterators, the most flexible solution will itself use an iterator. You could think of the TokenIterator as an iterator that wraps itself around any other iterator that can produce characters. Because it is certainly a type of input iterator (the most primitive type of iterator), it can provide input to any STL algorithm. Not only is it a useful tool in itself, the following TokenIterator is also a good example of how you can design your own iterators. Comment The TokenIterator class is doubly flexible. First, you can choose the type of iterator that will produce the char input. Second, instead of just saying what characters represent the delimiters, TokenIterator will use a predicate that is a function object whose operator( ) takes a char and decides whether it should be in the token. Although the two examples given here have a static concept of what characters belong in a token, you could easily design your own function object to change its state as the characters are read, producing a more sophisticated parser. The following header file contains two basic predicates, Isalpha and Delimiters, along with the template for TokenIterator: //: C07:TokenIterator.h #ifndef TOKENITERATOR_H #define TOKENITERATOR_H #include <algorithm> #include <cctype> #include <functional> #include <iterator> #include <string> struct Isalpha : std::unary_function<char, bool> { bool operator()(char c) { return std::isalpha(c); } }; class Delimiters : std::unary_function<char, bool> { std::string exclude; public: Delimiters() {} Delimiters(const std::string& excl) : exclude(excl) {} bool operator()(char c) { return exclude.find(c) == std::string::npos; } }; template <class InputIter, class Pred = Isalpha> class TokenIterator : public std::iterator< std::input_iterator_tag,std::string, std::ptrdiff_t> { InputIter first; InputIter last; std::string word; Pred predicate; public: TokenIterator(InputIter begin, InputIter end, Pred pred = Pred()) [97] 322 z 516 : first(begin), last(end), predicate(pred) { ++*this; } TokenIterator() {} // End sentinel // Prefix increment: TokenIterator& operator++() { word.resize(0); first = std::find_if(first, last, predicate); while (first != last && predicate(*first)) word += *first++; return *this; } // Postfix increment class Proxy { std::string word; public: Proxy(const std::string& w) : word(w) {} std::string operator*() { return word; } }; Proxy operator++(int) { Proxy d(word); ++*this; return d; } // Produce the actual value: std::string operator*() const { return word; } std::string* operator->() const { return &(operator*()); } // Compare iterators: bool operator==(const TokenIterator&) { return word.size() == 0 && first == last; } bool operator!=(const TokenIterator& rv) { return !(*this == rv); } }; #endif // TOKENITERATOR_H ///:~ The TokenIterator class derives from the std::iterator template. It might appear that some kind of functionality comes with std::iterator, but it is purely a way of tagging an iterator so that a container that uses it knows what it’s capable of. Here, you can see input_iterator_tag as the iterator_category template argument—this tells anyone who asks that a TokenIterator only has the capabilities of an input iterator and cannot be used with algorithms requiring more sophisticated iterators. Apart from the tagging, std::iterator doesn’t do anything beyond providing several useful type definitions, which means you must design all the other functionality in yourself. The TokenIterator class may look a little strange at first, because the first constructor requires both a “begin” and an “end” iterator as arguments, along with the predicate. Remember, this is a “wrapper” iterator that has no idea how to tell whether it’s at the end of its input source, so the ending iterator is necessary in the first constructor. The reason for the second (default) constructor is that the STL algorithms (and any algorithms you write) need a TokenIterator sentinel to be the past-the-end value. Since all the information necessary to see if the TokenIterator has reached the end of its input is collected in the first constructor, this second constructor creates a TokenIterator that is merely used as a placeholder in algorithms. The core of the behavior happens in operator++. This erases the current value of word using string::resize( ) and then finds the first character that satisfies the predicate (thus discovering [...]... C0 7: Stack3.cpp // Using a vector as a stack; modified Stack1.cpp #include #include #include #include using namespace std; int main() { ifstream in( "Stack3.cpp"); vector textlines; string line; while(getline (in, line)) 3 27 z 516 textlines.push_back(line + "\n"); while(!textlines.empty()) { cout . a starting point for performing some kind of source-code reformatting.) Comment //: C0 7: Stack2.cpp // Converting a list to a stack #include <iostream> #include <fstream> #include. of words used in a document #include <algorithm> #include <cctype> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <set> #include. priority: //: C0 7: PriorityQueue2.cpp // Changing the priority #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <queue> using namespace