在下面的代码中,函数(foo)参数(std::vector)的大小可以是任何值,以使函数成为泛型函数。但是,有时容器的大小是已知的,因此可以使用std::array。问题在于将std::array转换为std::vector。解决这个问题的最佳方法是什么?在这种情况下,总是使用std::vector是不是更好?
#include <iostream>
#include <array>
#include <vector>
using namespace std;
// generic function: size of the container can
#include<iostream>
#include<vector>
#include<cstdlib>
#include<thread>
#include<array>
#include<iterator>
#include<algorithm>
#include<functional>
#include<numeric>//accumulate
int sum_of_digits(int num, int sum = 0) {
//calculate sum of dig
我已经用C++创建了一个容器类,并且我有一个来自迭代器的构造函数,这样我就可以编写MyContainer<double> x(v.begin(), v.end()),其中v是一个std::vector<double>。我希望能够对c数组做同样的事情,但是:
double array[3] = {1., 2. , 3.};
MyContainer<double> x(array, array+3); // Doesn't work : no matching function for call to ‘MyContainer<double>
我想使用新的C++范式更新一个小型数学库(我有一个C++17编译器)。具体而言,我需要通过构造函数将数组传递给类。
这是我以前用过的“古典”方式。它起作用了
class Vector {
public:
long double* elements;
public:
Vector(int size, long double *array) {
elements = new long double[size];
for(int i = 0; i < size; i++) {
elements[i] = array[i];