Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组大小作为构造函数参数 我创建了一个C++类,它封装了浮点2D数组,并提供了一些附加功能。我想将数组的形状作为参数传递给构造函数,请参见下面的代码(类块部分)。以注释“//here”结尾的行在编译期间会导致错误,因为此时不知道_nx和_ny。 有两种解决方案(我认为):一种是使用指针(见下面代码中的解决方案1)并动态分配数组;另一种是使用模板(请参见下面代码中的解决方案2),但我有几个理由不使用它们: 我不想使用指针,只要有一个指针少 选项换句话说,我不想使用new和delete。这个 原因是个人偏好Puple C++。李> 我不想要 使用模板,因为可以有许多不同的块形状-I 不希望编译器为每个类创建许多类, 这是一种过激行为,增加了可执行文件的大小_C++_Arrays_Constructor - Fatal编程技术网

数组大小作为构造函数参数 我创建了一个C++类,它封装了浮点2D数组,并提供了一些附加功能。我想将数组的形状作为参数传递给构造函数,请参见下面的代码(类块部分)。以注释“//here”结尾的行在编译期间会导致错误,因为此时不知道_nx和_ny。 有两种解决方案(我认为):一种是使用指针(见下面代码中的解决方案1)并动态分配数组;另一种是使用模板(请参见下面代码中的解决方案2),但我有几个理由不使用它们: 我不想使用指针,只要有一个指针少 选项换句话说,我不想使用new和delete。这个 原因是个人偏好Puple C++。李> 我不想要 使用模板,因为可以有许多不同的块形状-I 不希望编译器为每个类创建许多类, 这是一种过激行为,增加了可执行文件的大小

数组大小作为构造函数参数 我创建了一个C++类,它封装了浮点2D数组,并提供了一些附加功能。我想将数组的形状作为参数传递给构造函数,请参见下面的代码(类块部分)。以注释“//here”结尾的行在编译期间会导致错误,因为此时不知道_nx和_ny。 有两种解决方案(我认为):一种是使用指针(见下面代码中的解决方案1)并动态分配数组;另一种是使用模板(请参见下面代码中的解决方案2),但我有几个理由不使用它们: 我不想使用指针,只要有一个指针少 选项换句话说,我不想使用new和delete。这个 原因是个人偏好Puple C++。李> 我不想要 使用模板,因为可以有许多不同的块形状-I 不希望编译器为每个类创建许多类, 这是一种过激行为,增加了可执行文件的大小,c++,arrays,constructor,C++,Arrays,Constructor,另外,我不想使用stl向量,因为数组大小在创建后是固定的;我也在做数值计算,所以“原始”数组更适合我 我在SO中搜索过,有五六个问题问了类似的问题,但没有结论哪一个更好,也没有一个是从数字的角度来看的,所以vector或new/detele对他们来说是很好的答案,但对我来说不是。我提出这个问题的另一个原因是我想知道我是否过于限制使用C++的特性。由于我将广泛使用C++,了解C++的局限性并停止对某些不存在的特征进行查询/搜索是非常重要的。 #include <iostream> #i

另外,我不想使用stl向量,因为数组大小在创建后是固定的;我也在做数值计算,所以“原始”数组更适合我

我在SO中搜索过,有五六个问题问了类似的问题,但没有结论哪一个更好,也没有一个是从数字的角度来看的,所以vector或new/detele对他们来说是很好的答案,但对我来说不是。我提出这个问题的另一个原因是我想知道我是否过于限制使用C++的特性。由于我将广泛使用C++,了解C++的局限性并停止对某些不存在的特征进行查询/搜索是非常重要的。
#include <iostream>
#include <memory>
using namespace std;

class Block
{
    public:
        Block(int nx, int ny):_nx(nx),_ny(ny){}
        void Report(void)
        {
            cout << "Block With Size ["<<_nx<<","<<_ny<<"]\n";
        }
    private:
        const int _nx, _ny;
        double _data[_nx][_ny]; // here
};


/// Solution 1, using auto_ptr
class BlockAuto
{
    public:
        BlockAuto(int nx, int ny):_nx(nx),_ny(ny),_data(new double[_nx*_ny]){}
        void Report(void)
        {
            cout << "BlockAuto With Size ["<<_nx<<","<<_ny<<"]\n";
        }
    private:
        const int _nx;
        const int _ny;
        const auto_ptr<double> _data;
};


/// Solution 2, using template
template<unsigned int nx, unsigned int ny>
class BlockTpl
{
    public:
        BlockTpl():_nx(nx),_ny(ny){}
        void Report(void)
        {
            cout << "BlockTpl With Size ["<<_nx<<","<<_ny<<"]\n";
        }
    private:
        const int _nx;
        const int _ny;
        double _data[nx][ny]; // uncomfortable here, can't use _nx, _ny
};

int main(int argc, const char *argv[])
{
    Block b(3,3);
    b.Report();

    BlockAuto ba(3,3);
    ba.Report();

    BlockTpl<3,4> bt;
    bt.Report();
    return 0;
}
#包括
#包括
使用名称空间std;
类块
{
公众:
块(intnx,intny):nx(nx),ny(ny){
作废报告(作废)
{

cout现在内存很便宜,而且块矩阵非常小

因此,当您不想使用模板,也不想使用动态分配时,可以使用一个固定大小的数组,该数组足够大,可以容纳尽可能大的块

就这么简单


使用
std::auto_ptr
显示的代码有两个主要问题:

  • std::auto_ptr
    在C++11中被弃用

  • std::auto_ptr
    始终执行
    delete p
    ,当分配为数组时,会产生未定义的行为,如
    new T[n]


顺便说一句,关于预想中的模板代码膨胀,如果您度量,您可能会感到惊喜


顺便说一下,这有点过早的优化。用C++来保持性能是一个好主意,不做不必要的缓慢或消耗内存的事情。但是,也不要陷入不必要的工作中,因为一些不重要的问题,或者根本没有问题。如果它被忽略了

因此,您的主要默认选择应该是使用std::vector作为存储


然后,如果你怀疑它太慢,度量。发布版本。哦,我只说过两次,所以这里是第三个:度量;-)

现在内存很便宜,块矩阵非常小

因此,当您不想使用模板,也不想使用动态分配时,可以使用一个固定大小的数组,该数组足够大,可以容纳尽可能大的块

就这么简单


使用
std::auto_ptr
显示的代码有两个主要问题:

  • std::auto_ptr
    在C++11中被弃用

  • std::auto_ptr
    始终执行
    delete p
    ,当分配为数组时,会产生未定义的行为,如
    new T[n]


顺便说一句,关于预想中的模板代码膨胀,如果您度量,您可能会感到惊喜


顺便说一下,这有点过早的优化。用C++来保持性能是一个好主意,不做不必要的缓慢或消耗内存的事情。但是,也不要陷入不必要的工作中,因为一些不重要的问题,或者根本没有问题。如果它被忽略了

因此,您的主要默认选择应该是使用std::vector作为存储


然后,如果你怀疑它太慢,测量。发布版本。哦,我只说过两次,所以这里是第三次:测量;-)

只需使用
std::vector
。一周前我也有同样的决策问题,我已经问过了

如果您使用
reserve()
,这将不会使您的
向量
多次重新分配自身(如果有),那么
向量
不会影响项目的性能。换句话说,
向量
不太可能成为您的瓶颈

请注意,在
C++
中,向量
被广泛使用,因此在
发布模式
中,对它们进行的优化非常有效

或者等待被引入!(不幸的是不是在
C++14
中,而是在
array TS
C++17
中),归功于manlio

永远不要忘记:过早的优化是邪恶的根源

不相信我?你不应该!你自己试试看

这是我的实验,让我相信,当我有完全相同的问题,你

实验代码:

#include <iostream>
#include <vector>
#include <ctime>
#include <ratio>
#include <chrono>

using namespace std;

int main() {
  const int N = 100000;


  cout << "Creating, filling and accessing an array of " << N << " elements.\n";

  using namespace std::chrono;

  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  int array[N];

  for(int i = 0; i < N; ++i)
    array[i] = i;

  for(int i = 0; i < N; ++i)
    array[i] += 5;

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  duration<double> time_span = duration_cast<duration<double>>(t2 - t1);

  std::cout << "It took me " << time_span.count() << " seconds.";
  std::cout << std::endl;


  cout << "Creating, filling and accessing an vector of " << N << " elements.\n";

  t1 = high_resolution_clock::now();

  vector<int> v;
  v.reserve(N);

  for(int i = 0; i < N; ++i)
    v.emplace_back(i);

  for(int i = 0; i < N; ++i)
    v[i] += 5;

  t2 = high_resolution_clock::now();

  time_span = duration_cast<duration<double>>(t2 - t1);

  std::cout << "It took me " << time_span.count() << " seconds.";
  std::cout << std::endl;

  return 0;
}
所以,只需一个
std::vector
:)我很确定您知道如何更改代码,您不需要我告诉您(当然是这样,请让我知道:)

你可以尝试其他的时间方法,可以在我的网站上找到samaras@samaras-A15:~$ g++ -std=gnu++0x -o2 px.cpp samaras@samaras-A15:~$ ./a.out Creating, filling and accessing an array of 100000 elements. It took me 0.002978 seconds. Creating, filling and accessing an vector of 100000 elements. It took me 0.002264 seconds.
class Block
{
public:
  BlockAuto(int p_rows, int p_cols):m_rows(nx),m_cols(ny)
  {
    m_vector.resize(m_rows*m_cols);
  }

  double at(uint p_x, uint p_y)
  {
    //Some check that p_x and p_y aren't over limit is advised
    return m_vector[p_x + p_y*m_rows];
  }
  void Report(void)
  {
    cout << "Block With Size ["<<_nx<<","<<_ny<<"]\n";
  }
private:
  const int m_rows;
  const int m_cols;
  std::vector<double> m_vector;
  //or double* m_data
};
class Block
{
    public:
        Block(int nx, int ny):_nx(nx),_ny(ny), _data(new double[_nx*_ny])
        {}
        void Report(void)
        {
            cout << "Block With Size ["<<_nx<<","<<_ny<<"]\n";
        }
    private:
        const int _nx, _ny;
        std::unique_ptr<double[]> _data; // here
};