[ create a new paste ] login | about

Link: http://codepad.org/bYxuDcdX    [ raw code | output | fork ]

arunksaha - C++, pasted on Oct 19:
// Arun Saha, 2010-Oct-19
// http://stackoverflow.com/questions/3970395/populate-c-maps-content-in-a-loop-scope

#include <set>
#include <map>
#include <iostream>

using std::cout;

typedef std::set< int > IntSet;
typedef std::map< int, IntSet > MapOfIntSet;

void print( IntSet const & );
void print( MapOfIntSet const & );

int main() {

    enum { N = 10 };

    MapOfIntSet c;     // container

    for( int i = 0; i < N; ++i ) {
        IntSet intSet;
        for( int j = i + 1; j < N; ++j ) {
            intSet.insert( j );
        }
        c.insert( make_pair( i, intSet ) );
    }

    print( c );

}

void print( MapOfIntSet const & mois ) {

    for( MapOfIntSet::const_iterator cit = mois.begin(); cit != mois.end(); ++cit ) {
        const int k = cit->first;
        const IntSet v = cit->second;
        cout << "{ " << k;
        print( v );
        cout << " }\n";
    }
}

void print( IntSet const & is ) {

    cout << " ( ";
    for( IntSet::const_iterator cit = is.begin(); cit != is.end(); ++cit ) {
        cout << *cit << " ";
    }
    cout << " )";
}


Output:
1
2
3
4
5
6
7
8
9
10
{ 0 ( 1 2 3 4 5 6 7 8 9  ) }
{ 1 ( 2 3 4 5 6 7 8 9  ) }
{ 2 ( 3 4 5 6 7 8 9  ) }
{ 3 ( 4 5 6 7 8 9  ) }
{ 4 ( 5 6 7 8 9  ) }
{ 5 ( 6 7 8 9  ) }
{ 6 ( 7 8 9  ) }
{ 7 ( 8 9  ) }
{ 8 ( 9  ) }
{ 9 (  ) }


Create a new paste based on this one


Comments: