//: C06:SpecialList.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Using the second version of transform(). #include #include #include #include "Inventory.h" #include "PrintSequence.h" using namespace std; struct Discounter { Inventory operator()(const Inventory& inv, float discount) { return Inventory(inv.getItem(), inv.getQuantity(), int(inv.getValue() * (1 - discount))); } }; struct DiscGen { float operator()() { float r = float(rand() % 10); return r / 100.0; } }; int main() { vector vi; srand(time(0)); // Randomize generate_n(back_inserter(vi), 15, InvenGen()); print(vi.begin(), vi.end(), "vi"); vector disc; generate_n(back_inserter(disc), 15, DiscGen()); print(disc.begin(), disc.end(), "Discounts:"); vector discounted; transform(vi.begin(),vi.end(), disc.begin(), back_inserter(discounted), Discounter()); print(discounted.begin(), discounted.end(),"discounted"); } ///:~