初始化std :: vector <std :: vector <int>&gt;使用boost :: assign :: list_of </std :: vector <int>

时间:2013-04-24 13:13:09

标签: c++ boost c++98

如何使用boost :: assign :: list_of将值放入std::vector<std::vector<int> >类型的对象中?或者你有任何其他的建议,我可以如何填充单元测试的目的?

已更新

我为gcc 4.4.2

收到以下编译器错误
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h: In static member function 'static _ForwardIterator std::__uninitialized_copy<<anonymous> >::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*, bool <anonymous> = false]':
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:117:   instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:257:   instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _ForwardIterator = std::vector<int, std::allocator<int> >*, _Tp = std::vector<int, std::allocator<int> >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:1024:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:1002:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:303:   instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>, _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:163:   instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert(const Container*, boost::assign_detail::default_type_tag) const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, DerivedTAssign = boost::assign_detail::generic_list<boost::assign_detail::generic_list<int> >, Iterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:142:   instantiated from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert_to_container() const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, DerivedTAssign = boost::assign_detail::generic_list<boost::assign_detail::generic_list<int> >, Iterator = std::_Deque_iterator<boost::assign_detail::generic_list<int>, boost::assign_detail::generic_list<int>&, boost::assign_detail::generic_list<int>*>]'
/boost/boost_1_51_0/boost/assign/list_of.hpp:436:   instantiated from 'boost::assign_detail::generic_list<T>::operator Container() const [with Container = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, T = boost::assign_detail::generic_list<int>]'
main.cc:1:   instantiated from here
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_uninitialized.h:74: error: call of overloaded 'vector(boost::assign_detail::generic_list<int>&)' is ambiguous
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:241: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:227: note:                 std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]
gcc/4.4.2/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:215: note:                 std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]

这是我的代码:

std::vector< std::vector<int> > v = boost::assign::list_of
                                    ( boost::assign::list_of(1)(2) )
                                    ( boost::assign::list_of(3) )
                                    ( boost::assign::list_of(5)(6)(7)(8) );

1 个答案:

答案 0 :(得分:3)

#include <boost/range/algorithm.hpp>
#include <boost/foreach.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <ostream>
#include <vector>
int main()
{
    using namespace boost;
    using namespace assign;
    using namespace std;

    vector< vector<int> > v = list_of
        ( list_of(1)(2) )
        ( list_of(3) )
        ( list_of(5)(6)(7)(8) );

    BOOST_FOREACH(const vector<int> &x, v)
    {
        copy(x,ostream_iterator<int>(cout," "));
        cout << endl;
    }
}

输出是:

1 2
3
5 6 7 8

  

我为gcc 4.4.2

收到以下编译器错误

Follwing代码适用于GCC 4.4.6:

vector< vector<int> > v;
v += list_of(1)(2),
     list_of(3),
     list_of(5)(6)(7)(8);