The standard library provides a variety of functions for dealing with containers such as vectors. This example illustrates simple sorting and searching a vector of objects. // Sorting and searching in a vector of class instances // If the vector is not too long, you could use find(), or iterate over the // vector until you find a match. However, if the if the vector is long, // it is better to sort it first, because it is a lot more efficient // to find things in a sorted list. // For complex objects like a class, you have to provide a function that // specifies the sort order. #include #include #include #include using namespace std; // We will be saving a lot of these in a vector class Element { public: string name; string symbol; int atomic_number; Element(string name_in, string symbol_in, int atomic_number_in) : name(name_in), symbol(symbol_in), atomic_number(atomic_number_in) {} // By default, sort() etc. use this function, so we must define it bool operator<(const Element& e) const { return (atomic_number < e.atomic_number);} }; // Alternative to operator< above is to provide this as final argument to sort, lower_bound, etc. //bool element_smaller(Element e1, Element e2) { // return (e1.atomic_number < e2.atomic_number); //} // This gets filled by a lot of Elements in unspecified order vector mytable; // A little function to find elements by atomic number z // This assumes that mytable has already been sorted. void findelement(int z) { // The search functions like to return iterators: vector::iterator lower, upper, i; lower = lower_bound(mytable.begin(), mytable.end(), Element("Xxxx", "X", z)); upper = upper_bound(mytable.begin(), mytable.end(), Element("Xxxx", "X", z)); if (lower == upper) cout << "There is no element of atomic number " << z << endl; // Notice that upper is AFTER the last match else for (i = lower; i != upper; i++) { cout << i->symbol << " (list entry " << int(i - mytable.begin()) << ") has atomic number " << z << endl; } } int main() { Element e = Element("Actinium", "Ac", 89); mytable.push_back(e); e = Element("Aluminium", "Al", 13); mytable.push_back(e); e = Element("Antimony", "Sb", 51); mytable.push_back(e); e = Element("Argon", "Ar", 18); mytable.push_back(e); e = Element("Arsenic", "As", 33); mytable.push_back(e); e = Element("Astatine", "At", 85); mytable.push_back(e); e = Element("Avalon", "Av", 33); mytable.push_back(e); cout << "Original order" << endl; for (int i=0; i < mytable.size(); i++) { cout << i << "\t" << mytable[i].atomic_number << "\t" << mytable[i].name << ", " << mytable[i].symbol << endl; } // Sort the vector using my custom comparison function sort(mytable.begin(), mytable.end()); // Just for fun, print the vector this time using iterators cout << "Sorted order" << endl; for(vector::iterator i = mytable.begin(); i !=mytable.end(); i++) { cout << int(i-mytable.begin()) << "\t" << i->atomic_number << "\t" << i->name << ", " << i->symbol<< endl; } // Find elements with atomic number 33 (in my periodic table, there can be more than one) findelement(33); // Find elements with atomic number 17 (in my periodic table, there are none) findelement(17); // Find elements with atomic number 51 findelement(51); }