我正在尝试使用InputFragment
而不是InputFragment
得出与该代码完全相同的代码。指向浮点数组[4]的指针:
CalculatorActivity
我尝试了很多组合,但是找不到正确的语法。
new
答案 0 :(得分:1)
您可以创建一个帮助程序类,该类可以通过转换自动神奇地new
为您服务。
template <unsigned N = 0> struct NewAny;
template <unsigned N> struct NewAny {
template <typename T>
operator T * () const { return new T[N]; }
};
template <> struct NewAny<0> {
template <typename T>
operator T * () const { return new T; }
};
int main () {
float (**x)[4] = NewAny<2>();
delete[] x;
x = NewAny<>();
delete x;
}
在您的示例中:
float (**f)[4] = NewAny<2>();
for (int p = 0; p < 2; p++) {
f[p] = NewAny<3>();
}
现代C ++教导通过使用容器和智能指针来避免对动态分配的内存进行手动管理的容易出错的本质。您可以执行以下操作来创建D
维vector
:
template <typename T, unsigned D> struct Vectorate;
template <unsigned N, typename T, unsigned D>
struct Vectorate<T[N], D> {
typedef
typename Vectorate<std::array<T, N>, D>::type type;
};
template <typename T, unsigned D>
struct Vectorate {
typedef
typename Vectorate<std::vector<T>, D-1>::type type;
};
template <typename T>
struct Vectorate<T, 0> {
typedef T type;
};
在您的示例中:
Vectorate<float[4], 2>::type f;
f.resize(2);
for(auto &ff : f) ff.resize(3);
答案 1 :(得分:1)
使用旧的声明(不要这样做):
from selenium import webdriver
driver = webdriver.Chrome("d:/chromedriver/Chromedriver.exe")
driver.get("https://www.google.com")
好一点(避免使用很多分配):
try
{
float *** f = new float ** [2];
for (size_t i = 0; i < 2; i++)
{
f[i] = new float * [3];
for (size_t j = 0; j < 3; j++)
f[i][j] = new float[4];
}
} catch(const std::exception & e) { /* This is a pain to deal with to avoid any leak */ }
// Now you can access via f[0][1][2]
// delete[] in reverse order.
有点麻烦,但是您不再关心内存泄漏:
try
{
typedef float BigArray[3][4];
BigArray * f = new BigArray[2];
} catch(const std::exception & e) { /* here it's simple */ }
// delete with delete[] f
而且,正如大多数人所说的那样,您应该将数组存储在float指针中,而不是将指针存储到float指针,这将使效率大大提高,因为您无需分3步取消引用访问元素,例如:
std::vector<std::vector<std::vector<float>>> f(2, std::vector<std::vector<float>>(3, std::vector<float>(4)));
// Use with f[0][1][2]
还有一些模板矩阵类(例如opencv中的示例)通过提供重载()运算符来正确执行此操作,因此您可以访问int vectorElems = 4, columns = 3, rows = 2;
int rowStride = vectorElems * columns;
float * f = new float[rows*columns*vectorElems];
// Access with stride:
f[0 * rowStride + 1 * vectorElems + 2] = 3.14f;
// Delete with delete[] f
答案 2 :(得分:-4)
我不确定... 但是尝试一下:
float **f[4];
for(int i = 0; i < 4; i++){
f[i] = (float**)malloc(sizeof(float**));
}
printf("%ld", sizeof(f));