开发者

c++ array question

I want to initialize an double array with size 200, and its value is from 0 to 199 from index 0 to 199 in c++. i开发者_JS百科 know i can do it by a simple For loop, but is there a way just to initialize the double array like this?

Thanks


Not really. A for loop is your best option:

double array[200];
for(int i = 0; i < 200; i++)
    array[i] = static_cast<double>(i);


Here's a way with std::generate:

template <typename T>
class nexter
{
public:
    nexter(T start = T())
        : value_(start)
    {
    }

    T operator()()
    {
        return value_++;
    }

private:
    T value_;
};

int main()
{
    double data[200];
    std::generate(data, data + 200, nexter<double>());
}

And if you were using C++0x, you could skip the functor:

int main()
{
    double data[200];
    double next = 0.0;
    std::generate(data, data + 200, [&next]() { return next++; } );
}


Using counting_iterator:

const int SIZE = 200;
double array[SIZE];
std::copy(counting_iterator<int>(0), counting_iterator<int>(SIZE), array);


Same as anthony-arnold, but with C++ vectors (or lists, or deque):

std::vector<double> array ;
array.reserve(200) ; // only for vectors, if you want
                     // to avoid reallocations

for(int i = 0; i < 200; i++)
    array.push_back(i) ;


If all value of array are same , you can do it easily. But values are different from each other so i don't think you do it directly.

double array[200];

for(int i=0 ; i<200 ; i++) { 
    array[i] = (double)i; 
}


If the 200 is a fixed constant and you don't want runtime overhead, I see basically two solutions.

For a C solution, you could solve it through the preprocessor. In think Boost has preprocessor for loops for such a thing. This would have the advantage to be done at compile time, no runtime overhead at all.

The constant 200 by itself might be a bit big for all compilers/preprocessors, though, but most modern ones should be able to handle this.

For a C++ solution, you could do it with template metaprogramming, I think. A recursive type with the number of elements in the template parameter could do the trick. But for constants that large the compile time overhead can be prohibitive.


The only way to initialize values of arrays is at the point of declarations (if the initializer is smaller than the array all other elements are initialized to zero;

double arr[5] = {0, 1, 2}; // arr = [0.0 ,1.0 ,2.0 ,0.0 ,0.0]

Otherwise there's no way to initialize the values and you'll have to loop over the array.

So what you can do is:

double arr[] = {0, 1, 2, 3, /* ... */, 199}; 

Although looping would be much better in most cases.


I think that for-loop is the simplest and most appropriate solution for your case. If you want just to know one more way to do it you could use std::transform:

#include <vector>
#include <algorithm>

double op_increase (double i) { return static_cast<double>(static_cast<int>(i)+1); }

int main() 
{ 
    std::vector<double> x( 200 ); // all elements were initialized to 0.0 here
    std::transform( x.begin(), x.end()-1, x.begin()+1, op_increase );

    return 0; 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜