0

boost::assign::list_of を使用して値を type のオブジェクトに入れるにはどうすればよい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) );
4

1 に答える 1

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 で次のコンパイラ エラーが発生します。

次のコードは GCC 4.4.6 で動作します:

vector< vector<int> > v;
v += list_of(1)(2),
     list_of(3),
     list_of(5)(6)(7)(8);
于 2013-04-24T13:25:23.540 に答える