Bjarne Stroustrup——《The Design and Evolution of C++》
vector<int> v; v += 1,2,3,4,5,6,7,8,9;
map<string,int> m; insert( m )( "Bar", 1 )( "Foo", 2 );
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/assert.hpp>;
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope
{
vector<int> values;
values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
BOOST_ASSERT( values.size() == 9 );
BOOST_ASSERT( values[0] == 1 );
BOOST_ASSERT( values[9] == 9 );
}
#include <boost/assign/list_inserter.hpp> // for 'insert()'
#include <boost/assert.hpp>
#include <string>
using namespace std;
using namespace boost::assign; // bring 'insert()' into scope
{
map<string,int> months;
insert( months )
( "january", 31 )( "february", 28 )
( "march", 31 )( "april", 30 )
( "may", 31 )( "june", 30 )
( "july", 31 )( "august", 31 )
( "september", 30 )( "october", 31 )
( "november", 30 )( "december", 31 );
BOOST_ASSERT( m.size() == 12 );
BOOST_ASSERT( m["january"] == 31 );
}
#include <boost/assign/list_inserter.hpp> // for 'push_front()'
#include <boost/assert.hpp>
#include <string>
#include <utility>
using namespace std;
using namespace boost::assign; // bring 'push_front()' into scope
{
typedef pair< string,string > str_pair;
deque<str_pair> deq;
push_front( deq )( "foo", "bar")( "boo", "far" );
BOOST_ASSERT( deq.size() == 2 );
BOOST_ASSERT( deq.front().first == "boo" );
BOOST_ASSERT( deq.back().second == "bar" );
} deque<int> di; push_front( di ) = 1,2,3,4,5,6,7,8,9; BOOST_ASSERT( di.size() == 9 ); BOOST_ASSERT( di[8] == 9 );
#include <boost/assign/list_of.hpp> // for 'list_of()'
#include <boost/assert.hpp>
#include <list>
#include <stack>
#include <string>
using namespace std;
using namespace boost::assign; // bring 'list_of()' into scope
{
const list<int> primes = list_of(1)(2)(3)(5)(7)(11);
BOOST_ASSERT( primes.size() == 6 );
BOOST_ASSERT( primes.back() == 11 );
BOOST_ASSERT( primes.front() == 1 );
const stack<string> names = list_of( "Mr. Foo" )( "Mr. Bar")( "Mrs. FooBar" ).to_adapter();
const stack<string> names2 = (list_of( "Mr. Foo" ), "Mr. Bar", "Mrs. FooBar" ).to_adapter();
BOOST_ASSERT( names.size() == 3 );
BOOST_ASSERT( names[0] == "Mr. Foo" );
BOOST_ASSERT( names[2] == "Mrs. FooBar" );
}
#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/assert.hpp>
#include <map>
using namespace std;
using namespace boost::assign; // bring 'map_list_of()' into scope
{
map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
BOOST_ASSERT( next.size() == 5 );
BOOST_ASSERT( next[ 1 ] == 2 );
BOOST_ASSERT( next[ 5 ] == 6 );
// or we can use 'list_of()' by specifying what type
// the list consists of
next = list_of< pair<int,int> >(6,7)(7,8)(8,9);
BOOST_ASSERT( next.size() == 3 );
BOOST_ASSERT( next[ 6 ] == 7 );
BOOST_ASSERT( next[ 8 ] == 9 );
}
#include <boost/assign/list_of.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/assert.hpp>
using namespace std;
using namespace boost::assign;
{
vector<int> v;
v += 1,2,3,repeat(10,4),5,6,7,8,9;
// v = [1,2,3,4,4,4,4,4,4,4,4,4,4,5,6,7,8,9]
BOOST_ASSERT( v.size() == 3 + 10 + 5 );
v = list_of(1).repeat(5,2)(3);
// v = [1,2,2,2,2,2,3]
BOOST_ASSERT( v.size() == 1 + 5 + 1 );
push_back( v )(1).repeat(1,2)(3);
// v = old v + [1,2,3]
BOOST_ASSERT( v.size() == 10 );
}
#include <boost/assign/std/vector.hpp>
#include <boost/assert.hpp>
#include <cstdlib> // for 'rand()'
using namespace std;
using namespace boost::assign;
template< class T >
struct next
{
T seed;
next( T seed ) : seed(seed)
{ }
T operator()() const
{
return seed++;
}
};
{
vector<int> v;
v += 1,2,repeat_fun(4,&rand),4;
// v = [1,2,?,?,?,?,4]
BOOST_ASSERT( v.size() == 7 );
push_back( v ).repeat_fun(4,next<int>(0))(4).repeat_fun(4,next<int>(5));
// v = old v + [0,1,2,3,4,5,6,7,8]
BOOST_ASSERT( v.size() == 16 );
}
#include <boost/assign/list_of.hpp>
#include <boost/assign/list_inserter.hpp>
#include <boost/assert.hpp>
#include <string>
#include <vector>
using namespace std;
using namespace boost::assign;
{
typedef vector<int> score_type;
typedef map<string,score_type> team_score_map;
typedef pair<string,score_type> score_pair;
team_score_map group1, group2;
//
// method 1: using 'insert()'
//
insert( group1 )( "Denmark", list_of(1)(1) )
( "Germany", list_of(0)(0) )
( "England", list_of(0)(1) );
BOOST_ASSERT( group1.size() == 3 );
BOOST_ASSERT( group1[ "Denmark" ][1] == 1 );
BOOST_ASSERT( group1[ "Germany" ][0] == 0 );
//
// method 2: using 'list_of()'
//
group2 = list_of< score_pair >
( "Norway", list_of(1)(0) )
( "USA", list_of(0)(0) )
( "Andorra", list_of(1)(1) );
BOOST_ASSERT( group2.size() == 3 );
BOOST_ASSERT( group2[ "Norway" ][0] == 1 );
BOOST_ASSERT( group2[ "USA" ][0] == 0 );
}| Header | Includes |
|---|---|
| <boost/assign.hpp> | everything |
| <boost/assign/list_of.hpp> | list_of(), map_list_of() |
| <boost/assign/std.hpp> | operator+=() for all standard containers (see below) |
| <boost/assign/std/deque.hpp> | operator+=() for std::deque, <deque> |
| <boost/assign/std/list.hpp> | operator+=() for std::list, <list> |
| <boost/assign/std/map.hpp> | operator+=() for std::map and std::multimap , <map> |
| <boost/assign/std/queue.hpp> | operator+=() for std::queue and std::priority_queue, <queue> |
| <boost/assign/std/set.hpp> | operator+=() for std::set and std::multiset, <set> |
| <boost/assign/std/slist.hpp> | operator+=() for std::slist if the class is available , <slist> |
| <boost/assign/std/stack.hpp> | operator+=() for std::stack, <stack> |
| <boost/assign/std/vector.hpp> | operator+=() for std::vector, <vector> |
| <boost/assign/assignment_exception.hpp> | Class assignment_exception which might be thrown by the proxy returned by list_of() |
| <boost/assign/list_inserter.hpp> | Functions make_list_inserter(), push_back(), push_front(),insert(), push() and class list_inserter which is the back-bone of this entire library. |
namespace boost
{
namespace assign
{
template< class V, class A, class V2 >
list_inserter< ... > operator+=( std::deque<V,A>& c, V2 v );
template< class V, class A, class V2 >
list_inserter< ... > operator+=( std::list<V,A>& c, V2 v );
template< class K, class V, class C, class A, class P >
list_inserter< ... > operator+=( std::map<K,V,C,A>& m, const P& p );
template< class K, class V, class C, class A, class P >
list_inserter< ... > operator+=( std::multimap<K,V,C,A>& m, const P& p );
template< class V, class C, class V2 >
list_inserter< ... > operator+=( std::queue<V,C>& c, V2 v );
template< class V, class C, class V2 >
list_inserter< ... > operator+=( std::priority_queue<V,C>& c, V2 v );
template< class K, class C, class A, class K2 >
list_inserter< ... > operator+=( std::set<K,C,A>& c, K2 k );
template< class K, class C, class A, class K2 >
list_inserter< ... > operator+=( std::multiset<K,C,A>& c, K2 k );
#ifdef BOOST_HAS_SLIST
template< class V, class A, class V2 >
list_inserter< ... > operator+=( std::slist<V,A>& c, V2 v );
#endif
template< class V, class C, class V2 >
list_inserter< ... > operator+=( std::stack<V,C>& c, V2 v );
template< class V, class A, class V2 >
list_inserter< ... > operator+=( std::vector<V,A>& c, V2 v );
} // namespace 'assign'
} // namespace 'boost'
namespace boost
{
namespace assign
{
template< class T >
class Implementation-defined
{
public:
const_iterator begin() const;
const_iterator end() const;
template< class U >
Implementation-defined& operator,( U u );
// inserts default-constructed object
Implementation-defined& operator()();
template< class U >
Implementation-defined& operator()( U u );
template< class U, class U2 >
Implementation-defined& operator()( U u, U2 u2 );
//
// and similarly up to 5 arguments
//
//
// Convert to a 'Container'. 'Container' must have a constructor
// which takes two iterators.
//
template< class Container >
operator Container() const;
//
// Convert to a container adapter like 'std::stack<>'.
//
Convertible-to-adapter to_adapter() const;
//
//
// Convert to eg. 'boost::array<T,std::size_t>'. If the
// assigned variable is too small,
// a assignment_exception is thrown.
// If the assigned variable it is too big, the rest of the
// values are default-constructed.
//
template< template <class,std::size_t> class Array, class U, std::size_t sz >
operator Array<U,sz>() const;
};
template< class T >
Implementation-defined list_of();
template< class T >
Implementation-defined list_of( T t );
template< class T, class U, class U2 >
Implementation-defined list_of( U u, U2 u2 );
template< class T, class U, class U2, class U3 >
Implementation-defined list_of( U u, U2 u2, U3 u3 );
template< class T, class U, class U2, class U3, class U4 >
Implementation-defined list_of( U u, U2 u2, U3 u3, U4 u4 );
template< class T, class U, class U2, class U3, class U4, class U5 >
Implementation-defined list_of( U u, U2 u2, U3 u3, U4 u4, U5 u5 );
template< class Key, class T >
Implementation-defined map_list_of( Key k, T t )
{
return list_of< std::pair<Key,T> >()( k, t );
}
} // namespace 'assign'
} // namespace 'boost'
namespace boost
{
namespace assign
{
template< Function, Argument = void >
class list_inserter
{
Function fun;
public:
explicit list_inserter( Function fun );
// conversion constructor
template< class Function2, class Arg >
list_inserter( const list_inserter<Function2,Arg>& );
public:
template< class U >
list_inserter& operator,( U u );
template< class U >
list_inserter& operator=( U u );
// calls 'fun()' with default-constructed object
list_inserter& operator()();
template< class U >
list_inserter& operator()( U u );
template< class U, class U2 >
list_inserter& operator()( U u, U2 u2 )
{
//
// if 'Argument' is 'void'
// fun( u, u2 );
// else
// fun( Argument( u, u2 ) );
//
return *this;
}
//
// similarly up to 5 arguments
//
};
template< class C >
list_inserter< ... > push_back( C& );
template< class C >
list_inserter< ... > push_front( C& );
template< class C >
list_inserter< ... > insert( C& );
template< class C >
list_inserter< ... > push( C& );
} // namespace 'assign'
} // namespace 'boost'
namespace boost
{
namespace assign
{
template< class Function >
list_inserter<Function> make_list_inserter( Function fun )
{
return list_inserter<Function>( fun );
}
}
} #define BOOST_ASSIGN_MAX_PARAMS 10 #include <boost/assign.hpp>
namespace boost
{
namespace assign
{
class assignment_exception : public std::exception
{
public:
explicit assignment_exception( const char* what );
virtual const char* what() const throw();
};
}
}
template< class V, class A, class V2 >
inline list_inserter< assign_detail::call_push_back< std::vector<V,A> >, V >
operator+=( std::vector<V,A>& c, V2 v )
{
return make_list_inserter( assign_detail::call_push_back< std::vector<V,A> >( c ) )( v );
}
template< class C >
class call_push_back
{
C& c_;
public:
call_push_back( C& c ) : c_( c )
{ }
template< class T >
void operator()( T r )
{
c_.push_back( r );
}
};
//
// A class representing emails
//
class email
{
public:
enum address_option
{
check_addr_book,
dont_check_addr_book
};
private:
typedef std::map< std::string,address_option > address_map;
//
// Store list of persons that must be cc'ed
//
mutable address_map cc_list;
//
// This extra function-object will take care of the
// insertion for us. It stores a reference to a
// map and 'operator()()' does the work.
//
struct add_to_map
{
address_map& m;
add_to_map( address_map& m ) : m(m)
{}
void operator()( const std::string& name, address_option ao )
{
m[ name ] = ao;
}
};
public:
//
// This function constructs the appropriate 'list_inserter'.
// Again we could have use 'boost::function', but it is
// trivial to use a function object.
//
// Notice that we do not specify an extra template
// parameter to 'list_inserter'; this means we forward
// all parameters directly to the function without
// calling any constructor.
//
list_inserter< add_to_map >
add_cc( std::string name, address_option ao )
{
//
// Notice how we pass the arguments 'name' and 'ao' to
// the 'list_inserter'.
//
return make_list_inserter( add_to_map( cc_list ) )( name, ao );
}
};
//
// Now we can use the class like this:
//
email e;
e.add_cc( "Mr. Foo", email::dont_check_addr_book )
( "Mr. Bar", email::check_addr_book )
( "Mrs. FooBar", email::check_addr_book );
{
using namespace std;
using namespace boost;
using namespace boost::assign;
vector<int> v = list_of(1)(2)(3)(4).to_container( v );
set<int> s = list_of(1)(2)(3)(4).to_container( s );
map<int,int> m = map_list_of(1,2)(2,3).to_container( m );
stack<int> st = list_of(1)(2)(3)(4).to_adapter( st );
queue<int> q = list_of(1)(2)(3)(4).to_adapter( q );
array<int,4> a = list_of(1)(2)(3)(4).to_array( a );
}map<int,int> next; insert( next )(1,2)(2,3); // compile-time error
map<int,int> next = map_list_of(1,2)(2,3);