[英]Populating a two dimension static vector using C++98 standard
I have a two-dimensional static vector ( std::vector< std::vector<double> >
) which needs to be populated and I am working on an old project which requires to be compiled using C++98. 我有一个二维静态向量( std::vector< std::vector<double> >
),需要填充它,并且我正在处理一个旧项目,该项目需要使用C ++ 98进行编译。 Therefore, I am not allowed to use std::vector<...> v = { {1,2}, {3,4} };
因此,不允许使用std::vector<...> v = { {1,2}, {3,4} };
syntax. 句法。
For unidimensional vectors, assigning an array as double a[] = {1,2};
对于一维向量,将数组分配为double a[] = {1,2};
精度double a[] = {1,2};
and then using the std::vector<double> v(a, a+2)
does the trick; 然后使用std::vector<double> v(a, a+2)
达到目的; however, it is not working for two dimensional vectors. 但是,它不适用于二维向量。
std::vector< std::vector<double> >
x1_step_lw_2(__x1_step_lw_2,
__x1_step_lw_2 + ARRAY_SIZE(__x1_step_lw_2));
I get the following error: 我收到以下错误:
../src/energyConsumption/ue-eennlite-model-param-7IN-30NN.cpp:193:33: required from here
/usr/include/c++/4.8/bits/stl_construct.h:83:7: error: invalid conversion from ‘const double*’ to ‘std::vector<double>::size_type {aka long \
unsigned int}’ [-fpermissive]
::new(static_cast<void*>(__p)) _T1(__value);
(ARRAY_SIZE(x) is a macro which calculates the size of an array) (ARRAY_SIZE(x)是一个宏,用于计算数组的大小)
And since these vectors are attributes of the class, It would make no sense to initiate them on a constructor. 并且由于这些向量是类的属性,因此在构造函数上初始化它们是没有意义的。
I am fighting against this problem for some time, and most of the 'solutions' involve to switch to C++11, which is not an option. 我在一段时间内一直在与这个问题作斗争,大多数“解决方案”都涉及切换到C ++ 11,这不是一个选择。
Any help is appreciated. 任何帮助表示赞赏。
My C++98 is rusty, but something like this should work: 我的C ++ 98生锈了,但类似的东西应该可以工作:
double a[] = { 1, 2 };
double b[] = { 3, 4 };
double c[] = { 5, 6 };
std::vector<double> v[] =
{
std::vector<double>(a, a + ARRAY_SIZE(a)),
std::vector<double>(b, b + ARRAY_SIZE(b)),
std::vector<double>(c, c + ARRAY_SIZE(c))
};
std::vector< std::vector<double> > vv(v, v + ARRAY_SIZE(v));
Try this: 尝试这个:
#include <iostream>
#include <vector>
/**this generates a vector of T type of initial size N, of course it is upto you whether you want to use N or not, and returns the vector*/
template<class T>
std::vector<T> generateInitVecs(size_t N)
{
std::vector<T> returnable;
for(int i = 0; i < N; i++)
returnable.push_back(0);
return returnable;
}
int main()
{
std::vector< std::vector<double> > twoDvector;
/**Since twoDvector stores double type vectors it will be first populated by double type vectors*/
for(int i = 0; i < 10; i++){
twoDvector.push_back(generateInitVecs<double>(10));
}
/**populating the vector of vectors*/
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++){
/**can be treated as 2D array*/
twoDvector[i][j] = 50;
}
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
std::cout << twoDvector[i][j] << " ";
}
std::cout << "\n";
}
}
It will print a 10 x 10 matrix, with all values assigned 50. 它将打印一个10 x 10矩阵,所有值均分配为50。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.