Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


/* Boost.MultiIndex example of use of multi_index_container::ctor_args_list.
 * Copyright 2003-2008 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * See for library home page.

#if !defined(NDEBUG)

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>

using boost::multi_index_container;
using namespace boost::multi_index;

/* modulo_less order numbers according to their division residual.
 * For instance, if modulo==10 then 22 is less than 15 as 22%10==2 and
 * 15%10==5.

template<typename IntegralType>
struct modulo_less
  modulo_less(IntegralType m):modulo(m){}

  bool operator()(IntegralType x,IntegralType y)const
    return (x%modulo)<(y%modulo);

  IntegralType modulo;

/* multi_index_container of unsigned ints holding a "natural" index plus
 * an ordering based on modulo_less.

typedef multi_index_container<
  unsigned int,
    ordered_unique<identity<unsigned int> >,
    ordered_non_unique<identity<unsigned int>, modulo_less<unsigned int> >
> modulo_indexed_set;

int main()
  /* define a modulo_indexed_set with modulo==10 */

  modulo_indexed_set::ctor_args_list args_list=
      /* ctor_args for index #0 is default constructible */

      /* first parm is key_from_value, second is our sought for key_compare */
      boost::make_tuple(identity<unsigned int>(),modulo_less<unsigned int>(10))

  modulo_indexed_set m(args_list);
  /* this could have be written online without the args_list variable,
   * left as it is for explanatory purposes. */

  /* insert some numbers */

  unsigned int       numbers[]={0,1,20,40,33,68,11,101,60,34,88,230,21,4,7,17};
  const std::size_t  numbers_length(sizeof(numbers)/sizeof(numbers[0]));


  /* lists all numbers in order, along with their "equivalence class", that is,
   * the equivalent numbers under modulo_less

  for(modulo_indexed_set::iterator it=m.begin();it!=m.end();++it){
    std::cout<<*it<<" -> ( ";

    nth_index<modulo_indexed_set,1>::type::iterator it0,it1;
    std::copy(it0,it1,std::ostream_iterator<unsigned int>(std::cout," "));


  return 0;