/* * Copyright 2011, 2013, 2025 Steven Gribble, Hal Perkins, Naomi Alterman * * This file is the solution to an exercise problem posed during * one of the UW CSE 333 lectures (333exercises). * * 333exercises is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 333exercises is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 333exercises. If not, see . */ // STL lecture exercise 1 // // Using the Amoeba.cc/.h file from lecture, construct a vector of // lists of Amoebas // // - i.e., a vector container, where each element is a list of Amoebas // // Observe how many copies happen when you: // - use the “sort” algorithm to sort the vector // - use the “list.sort( )” function to sort each list #include // for EXIT_SUCCESS #include // for std::vector #include // for std::list #include // for std::sort #include // for std::cout, std::endl; #include "./Amoeba.h" // for the Amoeba class using std::vector; using std::list; using std::sort; using std::for_each; const char *names[] = { "Margaret", "Ursula", "Joyce", "Emily", "Anne", "Charlotte", "Jane", "Kelly", "Lauren", "Virginia", "Tamora", "Susanna" }; int main(int argc, char **argv) { vector > vlp; // A three-list vector, with 4 Amoebas per list. int next_name = 0; for (int i = 0; i < 3; i++) { list list; for (int j = 0; j < 4; j++) { list.push_back(Amoeba(names[next_name++])); } vlp.push_back(list); } std::cout << (vlp[0] < vlp[1]) << std::endl; // Do the sorts. // Sort the vector. This means the sort algorithm is using // the "<" operator to compare elements of the vector. As it turns // out, C++'s STL defines overloads the "<" operator for when the // LHS and RHS are both lists! In other words, you can do // (list1 < list2)! See this for more info; it uses a lexicographical // comparison: // // http://www.cplusplus.com/reference/stl/list/operators/ // // http://www.cplusplus.com/reference/algorithm/lexicographical_compare/ sort(vlp.begin(), vlp.end()); // Sort each list in the vector. for (uint32_t i = 0; i < vlp.size(); i++) vlp[i].sort(); return EXIT_SUCCESS; }