如何在初始值设定项列表中创建非空提升 ublas::vector
How to create a nonempty boost ublas::vector inside an initializer list?
我正在尝试做这样的事情
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;
class A{
protected:
vector< double > a_;
public:
A( vector< double > a ) :
a_( a ) {};
};
class B : public A{
public:
B() : A( vector< double >( { 1.25, 2.75, 3.34 } ) ){};
};
结果应该是,向量a_
被声明为包含a_[0]=1.25, a_[1]=2.75, a_[2]=3.34
的三向量。
此代码不起作用boost::numeric::ublas::vector
因为没有可以处理vector<double>( { 1.25, 2.75, 3.34 } )
的构造函数
我应该改用什么?也许是构造函数
vector (size_type size, const double &data)
从提升文档有帮助吗?
您可以将 ublas/vector 的默认存储类型从 unbounded_array 更改为 std::vector 以获得initializer_list支持或引入帮助程序函数来初始化成员:
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;
template <typename T>
unbounded_array<T> make_unbounded_array(std::initializer_list<T> list) {
unbounded_array<T> result(list.size());
for(unsigned i = 0; i < list.size(); ++ i)
result[i] = *(list.begin() + i);
return result;
}
class Example
{
public:
vector<double, std::vector<double>> v0;
vector<double> v1;
public:
Example()
: v0({ 1.25, 2.75, 3.34 }),
v1(make_unbounded_array({ 1.25, 2.75, 3.34 }))
{}
};
int main(int argc, char **argv) {
Example example;
std::cout
<< example.v0[0] << " == " << example.v1[0] << 'n'
<< example.v0[1] << " == " << example.v1[1] << 'n'
<< example.v0[2] << " == " << example.v1[2] << 'n'
;
}
相关文章:
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- 创建一个没有复制构造函数的类的 std::vector 的 std::vector
- 为什么 vector 的方法调整大小会创建一个额外的对象?
- 创建一个 const std::vector ,其中包含另一个 const std::vector 和其他值
- 将vector<vector<double>>从x86平台中创建的一个进程发送到x64中构建的另一个进程的最快方法是什么
- 从函数为 std::vector 创建自定义扩展
- 在从仅移动类型派生的类中定义析构函数在使用 std::vector emplace_back或push_back创建时会
- 动态创建 std::vector 并将其传递给另一个函数的各种方法
- 从函数应用程序创建 std::vector
- 从 std::vector 创建 OpenCV 垫子的有效方法
- 如何创建一个继承自 std::vector 的类
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C++ std::vector 创建对象然后添加对象与在向量中创建对象之间的区别?
- 在不调用构造函数的情况下创建 Vector 元素
- 稍后使用<Class>调用类构造函数创建 std::vector
- std::vector::p ush_back(T&&) 在直接传递返回 T&& 的函数时创建默认构造值 T
- 使用 ::std::vector 创建数组
- 为什么"new vector<int>[5]"不创建向量数组?
- 仅使用标准库创建"Vector of References"的标准做法