gpt4 book ai didi

c++ - 如何将两个代码组合成一个函数?

转载 作者:行者123 更新时间:2023-11-28 04:02:19 26 4
gpt4 key购买 nike

我的代码创建了数组,我需要为它实现删除,但我不知道如何漂亮而正确地完成它。

主要代码

template<class T1>
auto auto_vector(T1&& _Size) {
return new int64_t[_Size]{};
}
template <class T1, class... T2>
auto auto_vector(T1&& _Size, T2&&... _Last)
{
auto result = new decltype(auto_vector(_Last...))[_Size];
for (int64_t i = 0; i < _Size; ++i) {
result[i] = auto_vector(_Last...);
}
return result;
}

这是我要与第一个结合的代码

template <class T1, class T2, class T3, class T4>
void del_vector(T4& _Del, T1& _Size, T2& _Size2, T3& _Size3) {
for (ptrdiff_t i = 0; i < _Size3; ++i) {
for (ptrdiff_t k = 0; k < _Size2; ++k) {
delete _Del[i][k];
}
delete _Del[i];
}
delete _Del;
}
int main()
{
auto map1 = auto_vector(_Size, _Size2, _Size3);

auto map2 = auto_vector(3, 4, 5, 7);

del_vector(map1, _Size, _Size2, _Size3);

return 0;
}

我不喜欢这个选项,我想要类似的东西。

int main()
{
auto_vector map1(_Size, _Size2, _Size3);

del_vector map1(_Size, _Size2, _Size3);

//or so

auto_vector<_Size, _Size2, _Size3> map1;

del_vector<_Size, _Size2, _Size3> map1;

return 0;
}

我这样做的原因是因为我不能只用一个 vector 来实现同样的事情而且我不明白为什么 vector 不适用于动态数组,事实是我不知道确切的数据_Size, _Size2, _Size3 = ? 编译前。

因此我使用 new 并且我所做的一切都是为了他。

看数据测试是否对你有用

cout << " ---------TEST---------- " << endl;
for (ptrdiff_t i = 0; i < _Size3; ++i) {
for (ptrdiff_t k = 0; k < _Size2; ++k) {
for (ptrdiff_t j = 0; j < _Size; ++j) {
cout << map1[i][k][j] << " ";
}
cout << endl;
}
cout << endl;
}
cout << " ---------TEST---------- " << endl;

最佳答案

您的代码中有太多 操作。此外,del_vector 在您的首选版本中没有任何意义,因为任何体面的类都会在析构函数中释放其数据(以免它对其没有所有权)。

您需要的是创建一个类或模板类来包装这些内容。

#include <iostream>
#include <vector>
#include <array>
#include <type_traits>

using namespace std;

template<size_t index, size_t dim>
void ModifyArray(std::array<size_t,dim>& lcAarray){}

template<size_t index, size_t dim, typename... Args>
void ModifyArray(std::array<size_t, dim>& lcAarray, size_t arg, Args... args)
{
lcAarray[index] = arg;
ModifyArray<index+1>(lcAarray, args...);
}

template<typename... Args>
std::array<size_t, sizeof...(Args)> MakeArray(Args... args)
{
std::array<size_t, sizeof...(Args)> lclArray;
ModifyArray<0>(lclArray, args...);
return lclArray;
}

template< std::size_t dim >
class myMultiArray;

template<std::size_t dim, std::size_t crDim>
class MyMultiArrayIterator
{
public:
MyMultiArrayIterator(myMultiArray<dim>* multiArray, size_t index):
m_pMultiArray(multiArray),
m_index(index)
{}

template<size_t newDim = crDim+1, typename std::enable_if<newDim < dim, int>::type = 0>
MyMultiArrayIterator<dim, newDim> operator [] (size_t idx)
{
return MyMultiArrayIterator<dim, newDim>(m_pMultiArray, m_index + idx*m_pMultiArray->GetStep(crDim));
}

template<size_t newDim = crDim+1, typename std::enable_if<newDim == dim, int>::type = 0>
int& operator [] (size_t idx)
{
return m_pMultiArray->GetValue(m_index+idx*m_pMultiArray->GetStep(crDim));
}

private:
size_t m_index;
myMultiArray<dim>* m_pMultiArray;
};

template< std::size_t dim >
class myMultiArray
{
public:
myMultiArray() = default;

template<class... Args, typename std::enable_if<sizeof...(Args) == dim-1, int>::type = 0>
myMultiArray(size_t size0, Args... args)
{
m_sizes = MakeArray(size0, args...);

std::size_t uTotalSize = 1;
for (std::size_t i = 0; i < dim; i++)
{
m_steps[i] = uTotalSize;
uTotalSize *= m_sizes[i];
}
std::cout << uTotalSize << "\n";
m_data.resize(uTotalSize);
}
// resizes m_data to multiplication of sizes
int operator () (std::array < std::size_t, dim > indexes) const
{
return m_data[computeIndex(indexes)];
}
int &operator () (std::array < std::size_t, dim > indexes)
{
return m_data[computeIndex(indexes)];
}
// modify operator
// you'll probably need more utility functions for such a multi dimensional array

int GetValue(size_t index) const
{
return m_data[index];
}

int &GetValue(size_t index)
{
return m_data[index];
}

size_t GetSize(size_t index) const
{
return m_sizes[index];
}

size_t GetStep(size_t index) const
{
return m_steps[index];
}

MyMultiArrayIterator<dim, 1> operator [] (size_t index)
{
return MyMultiArrayIterator<dim, 1>(this, index*m_steps[0]);
}

private:
size_t computeIndex(std::array < std::size_t, dim > indexes)
{
size_t location = 0;
for(size_t i=0; i< dim; i++)
{
location += m_steps[i]*indexes[i];
}
return location;
}

private:
std::vector < int > m_data;
std::array < std::size_t, dim > m_sizes;
std::array < std::size_t, dim > m_steps;
};

template<typename... Args>
myMultiArray<sizeof...(Args)> MakeMyMultiArray(Args... args)
{
return myMultiArray<sizeof...(Args)>(args...);
}

int main ()
{
auto mapMA = MakeMyMultiArray(3,4,5);

mapMA({2ull,3ull,4ull}) = 7;
std::cout << mapMA({{2ull,3ull,4ull}}) << "\n";
std::cout << mapMA[2][3][4];
return 0;
}

关于c++ - 如何将两个代码组合成一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59294880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com