//: C06:SortedSearchTest.cpp // Test searching in sorted ranges. // NString #include <algorithm> #include <cassert> #include <ctime> #include <cstdlib> #include <cstddef> #include <fstream> #include <iostream> #include <iterator> #include <vector> #include "NString.h" #include "PrintSequence.h" #include "../require.h" using namespace std; int main(int argc, char* argv[]) { typedef vector<NString>::iterator sit; char* fname = "Test.txt"; if(argc > 1) fname = argv[1]; ifstream in(fname); assure(in, fname); srand(time(0)); cout.setf(ios::boolalpha); vector<NString> original; copy(istream_iterator<string>(in), istream_iterator<string>(), back_inserter(original)); require(original.size() >= 4, "Must have four elements"); vector<NString> v(original.begin(), original.end()), w(original.size() / 2); sort(v.begin(), v.end()); print(v.begin(), v.end(), "sort"); v = original; stable_sort(v.begin(), v.end()); print(v.begin(), v.end(), "stable_sort"); v = original; sit it = v.begin(), it2; // Move iterator to middle for(size_t i = 0; i < v.size() / 2; i++) ++it; partial_sort(v.begin(), it, v.end()); cout << "middle = " << *it << endl; print(v.begin(), v.end(), "partial_sort"); v = original; // Move iterator to a quarter position it = v.begin(); for(size_t i = 0; i < v.size() / 4; i++) ++it; // Less elements to copy from than to the destination partial_sort_copy(v.begin(), it, w.begin(), w.end()); print(w.begin(), w.end(), "partial_sort_copy"); // Not enough room in destination partial_sort_copy(v.begin(), v.end(), w.begin(),w.end()); print(w.begin(), w.end(), "w partial_sort_copy"); // v remains the same through all this process assert(v == original); nth_element(v.begin(), it, v.end()); cout << "The nth_element = " << *it << endl; print(v.begin(), v.end(), "nth_element"); string f = original[rand() % original.size()]; cout << "binary search: " << binary_search(v.begin(), v.end(), f) << endl; sort(v.begin(), v.end()); it = lower_bound(v.begin(), v.end(), f); it2 = upper_bound(v.begin(), v.end(), f); print(it, it2, "found range"); pair<sit, sit> ip = equal_range(v.begin(), v.end(), f); print(ip.first, ip.second, "equal_range"); } ///:~
Listado 7.36. C06/SortedSearchTest.cpp
//: C06:MergeTest.cpp // Test merging in sorted ranges. //{L} Generators #include <algorithm> #include "PrintSequence.h" #include "Generators.h" using namespace std; int main() { const int SZ = 15; int a[SZ*2] = {0}; // Both ranges go in the same array: generate(a, a + SZ, SkipGen(0, 2)); a[3] = 4; a[4] = 4; generate(a + SZ, a + SZ*2, SkipGen(1, 3)); print(a, a + SZ, "range1", " "); print(a + SZ, a + SZ*2, "range2", " "); int b[SZ*2] = {0}; // Initialize all to zero merge(a, a + SZ, a + SZ, a + SZ*2, b); print(b, b + SZ*2, "merge", " "); // Reset b for(int i = 0; i < SZ*2; i++) b[i] = 0; inplace_merge(a, a + SZ, a + SZ*2); print(a, a + SZ*2, "inplace_merge", " "); int* end = set_union(a, a + SZ, a + SZ, a + SZ*2, b); print(b, end, "set_union", " "); } ///:~
Listado 7.37. C06/MergeTest.cpp
//: C06:SetOperations.cpp // Set operations on sorted ranges. //{L} Generators #include <algorithm> #include <vector> #include "Generators.h" #include "PrintSequence.h" using namespace std; int main() { const int SZ = 30; char v[SZ + 1], v2[SZ + 1]; CharGen g; generate(v, v + SZ, g); generate(v2, v2 + SZ, g); sort(v, v + SZ); sort(v2, v2 + SZ); print(v, v + SZ, "v", ""); print(v2, v2 + SZ, "v2", ""); bool b = includes(v, v + SZ, v + SZ/2, v + SZ); cout.setf(ios::boolalpha); cout << "includes: " << b << endl; char v3[SZ*2 + 1], *end; end = set_union(v, v + SZ, v2, v2 + SZ, v3); print(v3, end, "set_union", ""); end = set_intersection(v, v + SZ, v2, v2 + SZ, v3); print(v3, end, "set_intersection", ""); end = set_difference(v, v + SZ, v2, v2 + SZ, v3); print(v3, end, "set_difference", ""); end = set_symmetric_difference(v, v + SZ, v2, v2 + SZ, v3); print(v3, end, "set_symmetric_difference",""); } ///:~
Listado 7.38. C06/SetOperations.cpp