Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Extending the Standard Template Library with Association Classes


September 2001/Extending the Standard Template Library with Association Classes/Listing 3

Listing 3: Example of using ManyMany class with and without link attribute

#include <string>
#include "Relations.h"
#include <iostream>
using namespace std;

//Link attribute structure
//Uniquely identifies a student-class link
struct Record{

   Record(const string& grade = string(), int absenses = 0) 
      : Grade(grade), Absenses(absenses){}

   string Grade;
   int    Absenses;
};

main(){

   /*
   There is a many-to-many relationship between students and 
   classes: a student might be in many classes and a class might 
   have many students
   */

    //Many-To-Many relationship with a link attribute
    typedef ManyMany <string, long, less<string>, less<long>, 
                      Record> School_Rel;
    School_Rel school;

    Record r1("A", 1);
    school.insert("Peter", 101, r1);

    Record r2("B", 2);
    school.insert("Peter", 102, r2);

    Record r3("C", 3);
    school.insert("Peter", 103, r3);

    Record r4("A", 1);
    school.insert("Jane", 101, r4);

    Record r5("B", 1);
    school.insert("Jane", 103, r5);

    Record r6("A", 4);
    school.insert("Jane", 104, r6);

    cout << "Listing Peter's classes:" << endl;
    for(School_Rel::iterator2 it = school.begin("Peter"), 
        end = school.end("Peter"); it != end; ++it)
        cout << *it << endl;

    cout << endl;

    cout << "Listing students in 103: " << endl;
    for(School_Rel::iterator1 it = school.begin(103),
        end = school.end(103); it != end; ++it)
       cout << *it << endl;

    cout << endl;

    const Record* r = school.getAttribute("Peter", 103);

    if(r){
        cout << "Peter's  Grade and absenses for 103:" << endl;
        cout << "Grade - " << r->Grade << " Absenses - " 
             << r->Absenses << endl;
    }

    ////////////////////////////////////////////////////////////

    ////Many-To-Many relationship without a link attribute
    ManyMany <string, long> school1;

    school1.insert("Peter", 101);
    school1.insert("Peter", 102);
    school1.insert("Peter", 103);
    school1.insert("Jane", 101);
    school1.insert("Jane", 103);
    school1.insert("Jane", 104);

    cout << "Listing Jane's classes:" << endl;
    for(ManyMany <string, long> ::iterator2 it = 
        school1.begin("Jane"), end = school1.end("Jane");
        it != end; ++it)
        cout << *it << endl;

    cout << endl;

    cout << "Listing students in 101: " << endl;
    for(ManyMany <string, long>::iterator1 it = 
        school1.begin(101), end = school1.end(101);
        it != end; ++it)
        cout << *it << endl;
}

/*
Output:

Listing Peter's classes:
101
102
103

Listing students in 103:
Peter
Jane

Peter's  Grade and absenses for 103:
Grade - C Absenses - 3
Listing Jane's classes:
101
103
104

Listing students in 101:
Peter
Jane

*/
— End of Listing —

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.