我刚刚被以下内容灼伤。
如果我要初始化std::vector
的n
一个常量元素X
我这样做:
std::vector<double> v(n, X);
Run Code Online (Sandbox Code Playgroud)
但是,如果我需要初始化std::valarray
的n
一个常量元素X
我需要交换的大小和初始化常数:
std::valarray<double> va(X, n);
Run Code Online (Sandbox Code Playgroud)
在我看来,这就像一个任意的“陷阱”。
是否有技术原因或者决定有关的填充构造函数的参数的顺序时,当由标准委员会提供的一些设计原理std::vector
,并std::valarray
进行了规范?
我有一个类,它支持克隆(通过方法clone
)。我在 的向量中有一堆它的实例std::unique_ptr
。
现在,我想std::set
从上面的向量创建一个相同的智能指针,最好是在其构造过程中。明显的设计如下:
#include <memory>
#include <set>
#include <vector>
class A
{
public:
/// type of itself
typedef A self;
A() = default;
A(const self& another) = default;
virtual ~A() = default;
std::unique_ptr<A> clone() const
{
return std::make_unique<A>();
}
};
class SetOfA
{
public:
SetOfA() = default;
// Here is the method I would like to improve
SetOfA(const std::vector<std::unique_ptr<A> >& data)
{
//do not like this loop, prefer this to be in initialization part? …
Run Code Online (Sandbox Code Playgroud) 我有二维2D阵列(3D坐标),我想从中创建一个3D点矢量.当然,直接的方法是简单的循环,但是使用stl算法可能会更优雅的解决方案存在吗?这是我得到的:
#include <algorithm>
#include <iterator>
#include <vector>
struct point_3d
{
/**
* Default constructor -- set everything to 0
*/
point_3d() :
x(0.0), y(0.0), z(0.0)
{}
/**
* To define 3D point from array of doubles
*/
point_3d(const double crd[]) :
x(crd[0]),
y(crd[1]),
z(crd[2])
{}
/**
* To define 3D point from 3 coordinates
*/
point_3d(const double &_x, const double &_y, const double &_z) :
x(_x), y(_y), z(_z)
{}
double x, y, z;
}; //struct point_3d
//Right-angle tetrahedron
const int …
Run Code Online (Sandbox Code Playgroud)