Exemple #1
0
void demo_with_Things_custom_comparison()
{
	cout << "\ndemo_with_Things_custom_comparison" << endl;
	Thing t1(1);
	Thing t2(2);
	Thing t3(3);
	
	typedef Ordered_list<Thing> Thing_list_t;
	
	Thing_list_t thing_list(compare_Things_rev);

	cout << "thing_list size is " << thing_list.size() << endl;
	
	thing_list.insert(t3);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	thing_list.insert(t1);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	thing_list.insert(t2);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	// try out the find function
	for(int times = 0; times < 3; ++times) {
		cout << "Enter an int:";
		int i;
		cin >> i;
		// We have to search for the same kind of thing that is in the list, 
		// so we need to construct a "probe" object to use in the search.
		Thing probe(i);
		Thing_list_t::Iterator it = thing_list.find(probe);
		if(it == thing_list.end())
			cout << "Not found" << endl;
		else {
			cout << "Found - will remove" << endl;
			thing_list.erase(it);
			cout << "thing_list size is " << thing_list.size() << endl;
			print(thing_list);
			}
	}
	
	// call a member function on each Thing using the iterator arrow operator
	for(Thing_list_t::Iterator it = thing_list.begin(); it != thing_list.end(); it++) {
		it->increment();
	}
	cout << "incremented thing_list" << endl;
	print(thing_list);
	
	thing_list.clear();
	cout << "thing_list size is " << thing_list.size() << endl;
}
Exemple #2
0
void demo_with_Things_default_comparison()
{
	cout << "\ndemo_with_Things_default_comparison" << endl;
	/* now a list of Thing objects - each Thing constructed with an ID number */
	Thing t1(1);
	Thing t2(2);
	Thing t3(3);
	
	typedef Ordered_list<Thing> Thing_list_t;
	
// below uses an explicit comparison function
//	typedef bool(*Thing_cmp_t)(Thing, Thing);
//	Thing_list_t thing_list(compare_Things); // T is Thing

	Thing_list_t thing_list; // use default less-than function
	
	thing_list.insert(t3);
	thing_list.insert(t1);
	thing_list.insert(t2);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	// try out the find function
	for(int times = 0; times < 3; ++times) {
		cout << "Enter an int:";
		int i;
		cin >> i;
		// We have to search for the same kind of thing that is in the list, 
		// so we need to construct a "probe" object to use in the search.
		Thing probe(i);
		Thing_list_t::Iterator it = thing_list.find(probe);
		if(it == thing_list.end())
			cout << "Not found" << endl;
		else {
			cout << "Found - will remove" << endl;
			thing_list.erase(it);
			cout << "thing_list size is " << thing_list.size() << endl;
			print(thing_list);
			}
	}
	
	// call a member function on each Thing using the iterator arrow operator
	for(Thing_list_t::Iterator it = thing_list.begin(); it != thing_list.end(); it++) {
		it->increment();
	}
	cout << "incremented thing_list" << endl;
	print(thing_list);
	
	thing_list.clear();
	cout << "thing_list size is " << thing_list.size() << endl;
}	
void demo_with_Things_custom_comparison()
{
	cout << "\ndemo_with_Things_custom_comparison" << endl;
	Thing t1(1);
	Thing t2(2);
	Thing t3(3);
	
	typedef Ordered_list<Thing, Compare_Things_rev> Thing_list_t;
	
	Thing_list_t thing_list;

	cout << "thing_list size is " << thing_list.size() << endl;
	
	thing_list.insert(t3);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	thing_list.insert(t1);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	thing_list.insert(t2);
	cout << "thing_list size is " << thing_list.size() << endl;
	print(thing_list);
	
	// try out the find function
	for(int times = 0; times < 3; ++times) {
		cout << "Enter an int:";
		int i;
		cin >> i;
		// We have to search for the same kind of thing that is in the list, 
		// so we need to construct a "probe" object to use in the search.
		Thing probe(i);
		Thing_list_t::Iterator it = thing_list.find(probe);
		if(it == thing_list.end())
			cout << "Not found" << endl;
		else {
			cout << "Found - will remove" << endl;
			thing_list.erase(it);
			cout << "thing_list size is " << thing_list.size() << endl;
			print(thing_list);
			}
	}
	
	// call a member function on each Thing using the iterator arrow operator
	for(Thing_list_t::Iterator it = thing_list.begin(); it != thing_list.end(); it++) {
		it->increment();
	}
	cout << "incremented thing_list" << endl;
	print(thing_list);
	
	thing_list.clear();
	cout << "thing_list size is " << thing_list.size() << endl;
    
    // Demonstrate that the Ordering function type is part of the list type
    // create a default-constructed Ordered_list<Thing>
    // Ordered_list<Thing> x;
    //x = thing_list; // error - no assignment operator
    // Ordered_list<Thing> x(thing_list); // error - no matching constructor
    // Ordered_list<Thing> x(std::move(thing_list)); // error no matching constructor
    Ordered_list<Thing,Compare_Things_rev> x(thing_list); // fine - has the same type
    
    
}