代码之家  ›  专栏  ›  技术社区  ›  Mr Fooz

填充增压矢量或矩阵

  •  7
  • Mr Fooz  · 技术社区  · 17 年前

    是否有一种单一的表达方式将标量分配给boost矩阵或向量的所有元素?我试图找到一种更简洁的表达方式:

    boost::numeric::ublas::c_vector<float, N> v;
    for (size_t i=0; i<N; i++) {
        v[i] = myScalar;
     }
    

    以下各项不起作用:

    boost::numeric::ublas::c_vector<float, N> 
       v(myScalar, myScalar, ...and so on..., myScalar);
    
    boost::numeric::ublas::c_vector<float, N> v;
    v = myScalar;
    
    5 回复  |  直到 15 年前
        1
  •  8
  •   user21714    17 年前

    因为向量模型是一个标准的随机访问容器,所以您应该能够使用标准的STL算法。比如:

    c_vector<float,N> vec;
    std::fill_n(vec.begin(),N,0.0f);
    

    std::fill(vec.begin(),vec.end(),0.0f);
    

    它可能也与Boost.Assign兼容,但您必须进行检查。

        2
  •  6
  •   chrish    17 年前

    我已经开始使用 boost::assign

    #include <boost/assign/std/vector.hpp>
    using namespace boost::assign; // bring 'operator+()' into scope
    
    {
      vector<int> values;
      values += 1,2,3,4,5,6,7,8,9;
    }
    

    你也可以使用 boost::分配

    #include <boost/assign/list_inserter.hpp>
    #include <string>
    using boost::assign;
    
    std::map<std::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 );
    

    您可以允许使用直接分配 list_of() map_list_of()

    #include <boost/assign/list_of.hpp> // for 'list_of()'
    #include <list>
    #include <stack>
    #include <string>
    #include <map>
    using namespace std;
    using namespace boost::assign; // bring 'list_of()' into scope
    
    {
        const list<int> primes = list_of(2)(3)(5)(7)(11);
        const stack<string> names = list_of( "Mr. Foo" )( "Mr. Bar")
                                           ( "Mrs. FooBar" ).to_adapter();
    
        map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(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);
    
    }
    

    还有用于 repeat() , repeat_fun() range() 它允许您添加重复的值或值的范围。

        3
  •  5
  •   BloodAxe    14 年前

    建议的方式如下所示:

    boost::numeric::ublas::c_vector<float, N> v;
    v = boost::numeric::ublas::zero_vector<float>(N);
    v = boost::numeric::ublas::scalar_vector<float>(N, value);
    

    矩阵类型也是如此:

    boost::numeric::ublas::matrix<float> m(4,4);
    m = boost::numeric::ublas::identity_matrix<float>(4,4);
    m = boost::numeric::ublas::scalar_matrix<float>(4,4);
    m = boost::numeric::ublas::zero_matrix<float>(4,4);
    
        4
  •  1
  •   dario    15 年前

    ublas::c_向量v=ublas::标量_向量(N,myScalar);

        5
  •  0
  •   Mikko Rantanen    17 年前

    自从我使用C++以来,已经有一段时间了。下面的方法有效吗?

    for (size_t i = 0; i < N; v[i++] = myScalar) ;