填充一个boost向量或矩阵

7
有没有一种单表达式的方法可以将标量分配给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;

你也应该给它打上“C ++”标签。 - TonJ
5个回答

8

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

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

或者

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

它很可能也与 Boost.Assign 兼容,但你需要检查一下。

STL算法似乎是有效的。谢谢。Boost.Assign对我来说似乎不起作用,但我认为这是因为我使用了c_vector(固定大小的向量)而不是vector(动态大小的向量),所以push_back无法工作。 - Mr Fooz
1
这并没有回答如何填充 boost::numeric::ublas::matrix - quant

6
推荐的方式如下所示:
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);

如果我不知道值类型,该如何获取它? - quant_dev

6

我已开始使用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::assign来处理map。
#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()等函数,可以添加重复值或值的范围。


1
第一个例子似乎是针对std :: vector(它可以工作),而不是boost :: numeric :: ublas :: vector(它不能工作)。 - Daniel Newby

1
你试过这个吗? ublas::c_vector v = ublas::scalar_vector(N, myScalar);

0

我已经有一段时间没有使用C++了。以下代码是否有效?

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

那样做是可行的,尽管它是一个完整的语句而不是表达式。 - Mr Fooz
真的,但这是更紧凑的方式,这正是您根据帖子想要找到的。 - Mikko Rantanen
1
-1 抱歉。您必须使用 "v[i++]" -- "v[++i]" 会跳过 v[0] 的初始化并覆盖超出向量末尾的内存。 - j_random_hacker
抱歉!我承认我必须使用正确的一元运算符,但由于某种原因,我一直认为++i是在评估后递增i的。主要是因为每个人都喜欢i++,而“i += 1”的行为似乎更合理。无论如何,现在已经修复了。我想我也应该感谢你,我比601多获得了600分! - Mikko Rantanen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接