c++:struct变量中的某一项赋值给一个多维数组的例子
#include #include #include #include #include #include #include #include typedef double pixel_t; #include #include #include #include #include #include using namespace std; using namespace cv; int main() { ?? ?int m = 2; ?? ?int n = 3; ?? ?//double * b = new double[n];//动态分配一个有n个数据的数组 ?? ?double **b; ?//假定数组第一维长度为m, 第二维长度为n ?? ??? ?b = new double *[m];//动态分配空间 ?? ?for (int i = 0; i ?? ??? ?{ ?? ??? ? ? ? b[i] = new double[n]; ?? ??? ? } ?? ?//double b[2][3]; ?? ?size_t k = 2; ?? ?struct aa ?? ?{ ?? ??? ?double a[3]; ?? ?}; ?? ?vector f; ?? ?f.resize(k); ?? ?for (size_t i = 0; i < 3; i++) ?? ?{ ?? ??? ?f[0].a[i] = 1.5*i; ?? ??? ?f[1].a[i] = 0.5*i; ?? ?} ?? ?b[0][0] = *f[0].a; ?? ?b[0][1] = *(f[0].a + 1); ?? ?b[0][2] = *(f[0].a + 2); ?? ?b[1][0] = *f[1].a; ?? ?b[1][1] = *(f[1].a + 1); ?? ?b[1][2] = *(f[1].a + 2); ?? ?cout << *f[0].a << endl; ?? ?cout << *(f[0].a + 1) << endl; ?? ?cout << *(f[0].a + 2) << endl; ?? ?cout << *f[1].a << endl; ?? ?cout << *(f[1].a + 1) << endl; ?? ?cout << *(f[1].a + 2) << endl; ?? ?cout << b[0][0] << endl; ?? ?cout << b[0][1] << endl; ?? ?cout << b[0][2] << endl; ?? ?cout << b[1][0] << endl; ?? ?cout << b[1][1] << endl; ?? ?cout << b[1][2] << endl; ?? ?vector(f).swap(f); ?? ?//释放动态分配的数组 ?? ?for (int i = 0; i ?? ?{ ?? ??? ?delete[] b[i]; ?? ?}?? ? ?? ?delete b; ?? ?cout << b[1][1] << endl; ?? ?cout << f.size() << endl;//输出当前容器内存储元素个数 ?? ?cout << f.capacity() << endl; // 输出容器能存储数据的个数 ?? ?system("pause"); ?? ?return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |