0

我正在查看以下答案之一: 填充 boost 向量或矩阵 ,但我认为我是 boost(和 xcode,就此而言)的新手,并且正试图将我的头绕在 boost zero_vector 周围。

我尝试了一个简单的程序,我认为它与其中一个答案大致相同:

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main (int argc, char * const argv[]) {
    // insert code here...
    using namespace boost::numeric::ublas;

    int gameSize = 9;

    typedef vector<int> possiblesVector;
    possiblesVector foo;
    foo.resize(gameSize);
    foo = zero_vector<int>(gameSize);
    std::cout << foo << std::endl;

    return 0;
}

它可以编译,但是当它运行时,我得到一个运行时错误(用“/PATH/TO”代替真实路径)。

Check failed in file /PATH/TO/boost_1_48_0/boost/numeric/ublas/detail/vector_assign.hpp at line 370:
detail::expression_type_check (v, cv)
terminate called after throwing an instance of 'boost::numeric::ublas::external_logic'
  what():  external logic or bad condition of inputs
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

在这里,我只是使用一个 main.cpp 作为测试区域。在我的真实程序中,我将声明拆分为一个 .h 文件,并将我的初始化放在我的对象的一个​​ .cpp 文件中。但是上面的代码和我的真实程序一样失败。(即为什么我将声明和初始化分为两个步骤)

另外,我知道调整大小已经初始化为零。也许我会做一个 scalar_vector 代替,或者我可能需要稍后重置数组或其他东西。我只是想隔离正在破坏的代码。

4

1 回答 1

0
    template<template <class T1, class T2> class F, class V, class E>
    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    void vector_assign (V &v, const vector_expression<E> &e, sparse_tag) {
        BOOST_UBLAS_CHECK (v.size () == e ().size (), bad_size ());
        typedef F<typename V::iterator::reference, typename E::value_type> functor_type;
        BOOST_STATIC_ASSERT ((!functor_type::computed));
        typedef typename V::value_type value_type;
#if BOOST_UBLAS_TYPE_CHECK
        vector<value_type> cv (v.size ());
        indexing_vector_assign<scalar_assign> (cv, v);
        indexing_vector_assign<F> (cv, e);
#endif
        v.clear ();
        typename E::const_iterator ite (e ().begin ());
        typename E::const_iterator ite_end (e ().end ());
        while (ite != ite_end) {
            value_type t (*ite);
            if (t != value_type/*zero*/())
                v.insert_element (ite.index (), t);
            ++ ite;
        }
#if BOOST_UBLAS_TYPE_CHECK
        if (! disable_type_check<bool>::value) 
            BOOST_UBLAS_CHECK (detail::expression_type_check (v, cv), 
                               external_logic ("external logic or bad condition of inputs"));
#endif
    }

其中 v 是你的vector<int> possiblesVector,e 是临时的zero_vector<int>

// Weak equality check - useful to compare equality two arbitary vector expression results.
// Since the actual expressions are unknown, we check for and arbitary error bound
// on the relative error.
// For a linear expression the infinity norm makes sense as we do not know how the elements will be
// combined in the expression. False positive results are inevitable for arbirary expressions!
template<class E1, class E2, class S>
BOOST_UBLAS_INLINE
bool equals (const vector_expression<E1> &e1, const vector_expression<E2> &e2, S epsilon, S min_norm) {
    return norm_inf (e1 - e2) < epsilon *
           std::max<S> (std::max<S> (norm_inf (e1), norm_inf (e2)), min_norm);
}

template<class E1, class E2>
BOOST_UBLAS_INLINE
bool expression_type_check (const vector_expression<E1> &e1, const vector_expression<E2> &e2) {
    typedef typename type_traits<typename promote_traits<typename E1::value_type,
                                 typename E2::value_type>::promote_type>::real_type real_type;
    return equals (e1, e2, BOOST_UBLAS_TYPE_CHECK_EPSILON, BOOST_UBLAS_TYPE_CHECK_MIN);
}

检查功能。

zero_vector的所有元素都是0,所以之后

    v.clear ();
    typename E::const_iterator ite (e ().begin ());
    typename E::const_iterator ite_end (e ().end ());
    while (ite != ite_end) {
        value_type t (*ite);
        if (t != value_type/*zero*/())
            v.insert_element (ite.index (), t);
        ++ ite;
    }

向量 v 为空且检查失败。尝试使用doubleorfloat代替int

于 2012-07-19T02:35:24.167 回答