在初始化时预填充 std::vector ?

发布于 2024-09-05 04:10:24 字数 157 浏览 4 评论 0原文

我想创建一个 double 向量的向量向量,并希望它已经有 (32,32,16) 个元素,而不需要手动将所有这些元素推回去。有没有办法在初始化期间做到这一点? (我不在乎被推动的价值是什么)。

我想要一个三维数组,第一维有 32 个元素,第二维有 32 个元素,第三维有 16 个元素。

I want to create a vector of vector of a vector of double and want it to already have (32,32,16) elements, without manually pushing all of these back. Is there a way to do it during initialization? (I don't care what value gets pushed).

I want a 3-dimensional array, the first dimension has 32, the second dimension has 32 and the third dimension has 16 elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-09-12 04:10:24

一行:

std::vector< std::vector< std::vector<double> > > values(32, std::vector< std::vector<double> >(32, std::vector<double>(16, 0.0)));

或者打破界限:

typedef std::vector<double> v_type;
typedef std::vector<v_type> vv_type;
typedef std::vector<vv_type> vvv_type;

vvv_type values(32, vv_type(32, v_type(16, 0.0)));

我会说这分配了相当多的对象(32*32*16)。

单个向量有效吗?

std::vector<double> values(32*32*16, 0.0)

这将减少 32*32*16-1 new

One liner:

std::vector< std::vector< std::vector<double> > > values(32, std::vector< std::vector<double> >(32, std::vector<double>(16, 0.0)));

Or breaking the lines:

typedef std::vector<double> v_type;
typedef std::vector<v_type> vv_type;
typedef std::vector<vv_type> vvv_type;

vvv_type values(32, vv_type(32, v_type(16, 0.0)));

I would remark that this allocate a fair lot of objects (32*32*16).

Would a single vector work ?

std::vector<double> values(32*32*16, 0.0)

That would be 32*32*16-1 less new.

吻安 2024-09-12 04:10:24

向量的向量之一允许您指定要复制到向量元素中的大小和值。我不太确定“(32,32,16)”元素是什么意思,但你可以这样做:

// create a vector containing 16 elements set to 2
std::vector<int> temp(16, 2);   

// create a vector of 32 vectors, each with 16 elements set to 2
std::vector<std::vector<int> > values(32, temp);

One of the ctors for a vector allows you to specify both the size and a value to be copied into the elements for the vector. I'm not quite sure what you means by "(32,32,16)" elements, but you could do something like:

// create a vector containing 16 elements set to 2
std::vector<int> temp(16, 2);   

// create a vector of 32 vectors, each with 16 elements set to 2
std::vector<std::vector<int> > values(32, temp);
苦笑流年记忆 2024-09-12 04:10:24

给你:

vector<vector<vector<int> > > k(32, vector<vector<int> >(32, vector<int>(16,0)));

我什至不会问你为什么想要这样一个怪物,而不是仅仅使用一个带有你自己的索引方案的向量

Here you go:

vector<vector<vector<int> > > k(32, vector<vector<int> >(32, vector<int>(16,0)));

I am not even going to ask why you would want such a monstrosity and not just use a single vector with your own indexing scheme.

§普罗旺斯的薰衣草 2024-09-12 04:10:24

由于大小是事先已知的,并且您需要将整个数组转换为一维数组(对于 OpenGL),为什么不为其创建一个类呢?我创建了一个基于模板的 3D 数组实现,希望对您有用。

用法:

typedef Array3D<double, 3, 3, 3> DoubleArray333;
DoubleArray333 array;
Double val = 0.0;
for (size_t i = 0; i < DoubleArray333::SizeX; ++i)
{
 for (size_t j = 0; j < DoubleArray333::SizeY; ++j)
 {
  for (size_t k = 0; k < DoubleArray333::SizeZ; ++k)
  {
   array(i, j, k) = val++;
  }
 }
}
for (size_t i = 0; i < DoubleArray333::ArraySize; ++i)
{
 std::cout<<array[i]<<" ";
}

Array3D 头文件:

#pragma once

#include <exception>
#include <sstream>
#include <utility>
#include <memory>

/**
 * \brief   A 3D array of variable type and size.
 * \author  Vite Falcon
 * \date    08/06/2010
**/
template <typename T, int x, int y, int z>
class Array3D
{
private:
    T* m_array;

    /**
     * \brief   Validate the index range of different dimensions.
     * \remarks Vite Falcon, 08/06/2010.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \exception   std::out_of_range   Thrown when one of the indices is out-of-range.
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline void validateRange(size_t x, size_t y, size_t z)
    {
        if (x >= SizeX || y >= SizeY || z >= SizeZ)
        {
            std::stringstream ss;
            ss<<"Index out of range when accessing ("<<x<<", "<<y<<", "<<z<<
                ") when total size is ("<<SizeX<<", "<<SizeY<<", "<<SizeZ<<").";
            throw std::out_of_range(ss.str());
        }
    }

    /**
     * \brief   Validates the given array index.
     * \remarks Vite Falcon, 08/06/2010.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \exception   std::out_of_range   Thrown when the index is out-of-range.
     * \param   index   Zero-based index of the array.
    **/
    inline void validateIndex(size_t index)
    {
        if (index >= ArraySize)
        {
            std::stringstream ss;
            ss<<"Index out of range when accessing array by index "<<index<<
                " when total array size is "<<ArraySize<<".";
            throw std::out_of_range(ss.str());
        }
    }
public:
    static const size_t SizeX;
    static const size_t SizeY;
    static const size_t SizeZ;
    static const size_t ArraySize;

    typedef Array3D<T,x,y,z> MyType;

    /**
     * \brief   Default constructor. 
     * \author  Vite Falcon
     * \date    08/06/2010
    **/
    Array3D(void)
        :m_array(new T[ArraySize])
    {
    }

    /**
     * \brief   Copy constructor.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   other   The other.
    **/
    Array3D(const MyType& other)
        :m_array(new T[ArraySize])
    {
        memcpy_s(m_array, sizeof(T)*ArraySize, other.m_array, sizeof(T)*ArraySize);
    }

    /**
     * \brief   Destructor.
     * \author  Vite Falcon
     * \date    08/06/2010
    **/
    ~Array3D(void)
    {
        delete[] m_array;
        m_array = 0;
    }

    /**
     * \brief   Gets the value at a particular array index.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  The value at the given index.
    **/
    inline T& at(size_t array_index)
    {
        return (*this)[array_index];
    }

    /**
     * \brief   Gets the value at a particular array index (const version)
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  The value at the given index.
    **/
    inline const T& at(size_t array_index) const
    {
        return (*this)[array_index];
    }

    /**
     * \brief   Gets the value in the array from the given 3D indices.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
     * \return  The value at the given indices.
    **/
    inline T& at(size_t x, size_t y, size_t z)
    {
        return (*this)(x, y, z);
    }

    /**
    * \brief    Gets the value in the array from the given 3D indices (const version).
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
     * \return  The value at the given indices.
    **/
    inline const T& at(size_t x, size_t y, size_t z) const
    {
        return (*this)(x, y, z);
    }

    /**
     * \brief   The '()' operator to access the values as a 3D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The value at the given indices.
     *
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline T& operator ()(size_t x, size_t y, size_t z)
    {
        validateRange(x, y, z);
        return m_array[x*SizeY*SizeZ + y*SizeZ + z];
    }

    /**
     * \brief   The '()' operator to access the values as a 3D array (const version).
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The value at the given indices.
     *
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline const T& operator()(size_t x, size_t y, size_t z) const
    {
        validateRange(x, y, z);
        return m_array[x*SizeY*SizeZ + y*SizeZ + z];
    }

    /**
     * \brief   The '[]' operator to access the values as a 1D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  Value at the given index.
    **/
    inline T& operator[](size_t array_index)
    {
        validateIndex(array_index);
        return m_array[array_index];
    }

    /**
     * \brief   The '[]' operator to access the values as a 1D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  Value at the given index.
    **/
    inline const T& operator[](size_t array_index) const
    {
        validateIndex(array_index);
        return m_array[array_index];
    }

    /**
     * \brief   Fills the array with the given value.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   val The value to fill the array.
    **/
    void fill(const T& val)
    {
        for (size_t i = 0; i < ArraySize; ++i)
        {
            m_array[i] = val;
        }
    }

    /**
     * \brief   Gets the raw array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The 1D array.
    **/
    T* getArray()
    {
        return m_array;
    }

    /**
     * \brief   Swaps the current array with the given one.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param [in,out]  other   The other.
    **/
    void swap(MyType& other)
    {
        std::swap(m_array, other.m_array);
    }

    /**
     * \brief   Copy operator.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   other   The other.
     * \return  A shallow copy of this object.
    **/
    MyType& operator = (const MyType& other)
    {
        MyType temp(other);
        swap(temp);
        return *this;
    }
};

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeX = x;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeY = y;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeZ = z;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::ArraySize = x*y*z;

您还可以使用以下函数获取指向 1D 数组的指针:
double* double_array = array.getArray();< /code>

编辑:更改了用法以用 double 而不是 int 显示。

Since the size is known before hand and you will need to convert the whole array to a 1D array (for OpenGL) why not create a class for it? I have created a template-based implementation of a 3D array which I hope will be useful to you.

Usage:

typedef Array3D<double, 3, 3, 3> DoubleArray333;
DoubleArray333 array;
Double val = 0.0;
for (size_t i = 0; i < DoubleArray333::SizeX; ++i)
{
 for (size_t j = 0; j < DoubleArray333::SizeY; ++j)
 {
  for (size_t k = 0; k < DoubleArray333::SizeZ; ++k)
  {
   array(i, j, k) = val++;
  }
 }
}
for (size_t i = 0; i < DoubleArray333::ArraySize; ++i)
{
 std::cout<<array[i]<<" ";
}

The Array3D header file:

#pragma once

#include <exception>
#include <sstream>
#include <utility>
#include <memory>

/**
 * \brief   A 3D array of variable type and size.
 * \author  Vite Falcon
 * \date    08/06/2010
**/
template <typename T, int x, int y, int z>
class Array3D
{
private:
    T* m_array;

    /**
     * \brief   Validate the index range of different dimensions.
     * \remarks Vite Falcon, 08/06/2010.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \exception   std::out_of_range   Thrown when one of the indices is out-of-range.
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline void validateRange(size_t x, size_t y, size_t z)
    {
        if (x >= SizeX || y >= SizeY || z >= SizeZ)
        {
            std::stringstream ss;
            ss<<"Index out of range when accessing ("<<x<<", "<<y<<", "<<z<<
                ") when total size is ("<<SizeX<<", "<<SizeY<<", "<<SizeZ<<").";
            throw std::out_of_range(ss.str());
        }
    }

    /**
     * \brief   Validates the given array index.
     * \remarks Vite Falcon, 08/06/2010.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \exception   std::out_of_range   Thrown when the index is out-of-range.
     * \param   index   Zero-based index of the array.
    **/
    inline void validateIndex(size_t index)
    {
        if (index >= ArraySize)
        {
            std::stringstream ss;
            ss<<"Index out of range when accessing array by index "<<index<<
                " when total array size is "<<ArraySize<<".";
            throw std::out_of_range(ss.str());
        }
    }
public:
    static const size_t SizeX;
    static const size_t SizeY;
    static const size_t SizeZ;
    static const size_t ArraySize;

    typedef Array3D<T,x,y,z> MyType;

    /**
     * \brief   Default constructor. 
     * \author  Vite Falcon
     * \date    08/06/2010
    **/
    Array3D(void)
        :m_array(new T[ArraySize])
    {
    }

    /**
     * \brief   Copy constructor.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   other   The other.
    **/
    Array3D(const MyType& other)
        :m_array(new T[ArraySize])
    {
        memcpy_s(m_array, sizeof(T)*ArraySize, other.m_array, sizeof(T)*ArraySize);
    }

    /**
     * \brief   Destructor.
     * \author  Vite Falcon
     * \date    08/06/2010
    **/
    ~Array3D(void)
    {
        delete[] m_array;
        m_array = 0;
    }

    /**
     * \brief   Gets the value at a particular array index.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  The value at the given index.
    **/
    inline T& at(size_t array_index)
    {
        return (*this)[array_index];
    }

    /**
     * \brief   Gets the value at a particular array index (const version)
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  The value at the given index.
    **/
    inline const T& at(size_t array_index) const
    {
        return (*this)[array_index];
    }

    /**
     * \brief   Gets the value in the array from the given 3D indices.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
     * \return  The value at the given indices.
    **/
    inline T& at(size_t x, size_t y, size_t z)
    {
        return (*this)(x, y, z);
    }

    /**
    * \brief    Gets the value in the array from the given 3D indices (const version).
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
     * \return  The value at the given indices.
    **/
    inline const T& at(size_t x, size_t y, size_t z) const
    {
        return (*this)(x, y, z);
    }

    /**
     * \brief   The '()' operator to access the values as a 3D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The value at the given indices.
     *
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline T& operator ()(size_t x, size_t y, size_t z)
    {
        validateRange(x, y, z);
        return m_array[x*SizeY*SizeZ + y*SizeZ + z];
    }

    /**
     * \brief   The '()' operator to access the values as a 3D array (const version).
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The value at the given indices.
     *
     * \param   x   The width index.
     * \param   y   The height index.
     * \param   z   The depth index.
    **/
    inline const T& operator()(size_t x, size_t y, size_t z) const
    {
        validateRange(x, y, z);
        return m_array[x*SizeY*SizeZ + y*SizeZ + z];
    }

    /**
     * \brief   The '[]' operator to access the values as a 1D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  Value at the given index.
    **/
    inline T& operator[](size_t array_index)
    {
        validateIndex(array_index);
        return m_array[array_index];
    }

    /**
     * \brief   The '[]' operator to access the values as a 1D array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   array_index Zero-based index of the array.
     * \return  Value at the given index.
    **/
    inline const T& operator[](size_t array_index) const
    {
        validateIndex(array_index);
        return m_array[array_index];
    }

    /**
     * \brief   Fills the array with the given value.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   val The value to fill the array.
    **/
    void fill(const T& val)
    {
        for (size_t i = 0; i < ArraySize; ++i)
        {
            m_array[i] = val;
        }
    }

    /**
     * \brief   Gets the raw array.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \return  The 1D array.
    **/
    T* getArray()
    {
        return m_array;
    }

    /**
     * \brief   Swaps the current array with the given one.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param [in,out]  other   The other.
    **/
    void swap(MyType& other)
    {
        std::swap(m_array, other.m_array);
    }

    /**
     * \brief   Copy operator.
     * \author  Vite Falcon
     * \date    08/06/2010
     * \param   other   The other.
     * \return  A shallow copy of this object.
    **/
    MyType& operator = (const MyType& other)
    {
        MyType temp(other);
        swap(temp);
        return *this;
    }
};

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeX = x;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeY = y;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::SizeZ = z;

template <typename T, int x, int y, int z>
const size_t Array3D<T, x, y, z>::ArraySize = x*y*z;

You can also get the pointer to the array as a 1D by using the function:
double* double_array = array.getArray();

EDIT: Changed the usage to show it with double rather than int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文