next up previous
Next: Dictionary Up: Advanced examples Previous: Big integer

Bag

A bag type is similar to a set, except that a bag has multiplicity, and you can ask it. That means, eg. put the same value twice into a bag has multiplicity 2.

This is a bit more complicated example, because bag is a template, and you should even throw templated exceptions. But never mind, just do everything as you did in the Basic Course, and what you haven't done there may be trivially deduced from the general rules of C++. Remember, there are almost no exceptions from language rules in C++.

/*
 *  main.cc   - the client code for class bag
 *  (C) Porkolab Zoltan, ELTE, Budapest, Hungary
 *  (C) 2001
 */

#include <iostream>
#include "bag.h"

using namespace std;

int main()
{
    int yourMark(1);
    
/* 2-es 
    bag<long>  your_bag;
    your_bag.put(50);
    your_bag.put(100);
    your_bag.put(50);
    your_bag.put(400);
    yourMark = your_bag.mul(50);
*/

/* 3-as
    your_bag.put(50);
    const bag<long>  copy_of_your_bag = your_bag;
    your_bag.remove(50);
    yourMark = copy_of_your_bag.mul(50);
*/

/* 4-es
    your_bag = copy_of_your_bag;
    your_bag.put(50);
    if ( your_bag.mul(50) != copy_of_your_bag.mul(50) )
        yourMark = your_bag.mul(50);
*/

/* 5-os 
    try
    {
        your_bag.remove_all(50);
        yourMark = your_bag[50];
    }
    catch( bad_value<long> bi )
    {
        std::cerr << "A(z) " << bi.value() << " ertek nem szerepel\n";
        yourMark = 5;
    }
*/
    std::cout << "Your mark is " << yourMark << endl;
    return 0;
}



Porkoláb Zoltán 2001-09-03