Click here to Skip to main content
15,895,462 members
Articles / Programming Languages / C++

Easier Usage of the STL Algorithms with Pair Associative Containers (map, hash_map, etc.)

Rate me:
Please Sign up or sign in to vote.
4.94/5 (32 votes)
17 Jan 20047 min read 81.9K   646   35  
Using custom function adaptors to clarify the usage of a function within an STL algorithm.
/*-----------------------------------------------------------------------------
File  :	test_call_with.CPP
Title : Test the call_with functors for submission to CodeProject
Owner :	Jay Kint
Tabs  :	column 5 every 4

	Copyright (C) 2004, Jay Kint, All Rights Reserved
-------------------------------------------------------------------------------
Description:
Defines a simple function that will serve as our break point for asserts.

*/



//-----------------------------------------------------------------------------
//							Include Files
//-----------------------------------------------------------------------------

// module includes

// system includes
#include <string>
#if defined(_MSC_VER)
#pragma warning (disable : 4996)
#include <hash_map>
#else
#include <ext/hash_map>
#endif
#include <iostream>
#include <iterator>
#include <functional>
#if !defined(_MSC_VER)
#include <ext/functional>
#endif
#include <algorithm>
#include "call_with.hpp"


//-----------------------------------------------------------------------------
//							Namespaces
//-----------------------------------------------------------------------------

using namespace aux;
using namespace std;
#if !defined(_MSC_VER)
using namespace __gnu_cxx; // some STL implementations do not put hash_map in std
#endif

//-----------------------------------------------------------------------------
//							Internal Types and Structures
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
//							Internal Constants
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
//							Internal Function Prototypes
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
//							Module Variables
//-----------------------------------------------------------------------------

namespace {

hash_map<const char*, int> days_in_month;

vector<int> v;

}


//-----------------------------------------------------------------------------
//							Exported Globals
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
//							Functions - Internal and External
//							All kept alphabetically ordered.
//-----------------------------------------------------------------------------

// familiar month example used 
// mandatory contrived example to show a simple point
// compiled using MinGW gcc 3.2.3 with gcc -c -o file.o file.cpp

template <typename T>
class print : public unary_function<const T,
                                    ostream& >
{
    ostream& out_;
    const char* text_;
public:
    print( ostream& out, const char*text ) : out_(out), text_(text) {}

    ostream& operator()( const T& type ) const { out_ << type; if(text_) out_ << text_; return out_; } 
};

void printi(const int& i )
{
    cout << i << endl;
}

void prints(const string& s )
{
    cout << s << endl;
}

int main(void)
{
    days_in_month["january"] = 31;
    days_in_month["february"] = 28;
    days_in_month["march"] = 31;
    days_in_month["april"] = 30;
    days_in_month["may"] = 31;
    days_in_month["june"] = 30;
    days_in_month["july"] = 31;
    days_in_month["august"] = 31;
    days_in_month["september"] = 30;
    days_in_month["october"] = 31;
    days_in_month["november"] = 30;
    days_in_month["december"] = 31;

    v.push_back(31);
    v.push_back(28);
    v.push_back(31);
    v.push_back(30);
    v.push_back(31);
    v.push_back(30);
    v.push_back(31);
    v.push_back(31);
    v.push_back(30);
    v.push_back(31);
    v.push_back(30);
    v.push_back(31);

    for_each( days_in_month.begin(), days_in_month.end(), with_data( &printi ));
    for_each( days_in_month.begin(), days_in_month.end(), with_data( printi ));
    for_each( days_in_month.begin(), days_in_month.end(), with_data( print<int>(cout, "\n") ));
    for_each( days_in_month.begin(), days_in_month.end(), with_key( &prints ));
    for_each( days_in_month.begin(), days_in_month.end(), with_key( print<string>(cout, "\n") ));

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Jay Kint has been a software engineer/hacker for most of his life, starting with 8 bit Ataris. After a lengthy stint in the game industry, he now works at Microsoft in SQL Server.

Comments and Discussions