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 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);
counting_iterator solution is simple.std::copy is to be in a position not to judge C++ code, for one isn't a C++ programmer.boost::mpl).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++; } );
}
for_each isn't actually suppose to mutate the elements. Also, if you want to ignore a parameter just don't name it. Lastly, might as well use std::generate if you're gonna use a functor. :)std::generate - updated my example and also gave an example using C++0x lambdas.Using counting_iterator:
const int SIZE = 200;
double array[SIZE];
std::copy(counting_iterator<int>(0), counting_iterator<int>(SIZE), array);
::std:;copy should've been written to require the destination begin and end, and the source begin instead of the other way around. You can get buffer overflows either way, but harder to exploit when the overflow is only reading, not writing.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;
}
i = 200 as it will write to array[200]. Put the assignment to array[i] into the body of the for statement where it belongs!array[0] is not written and stays uninitialized.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.
double A[] = INIT_DARRAY(200);. Sure the implementation of such a macro ain't simple, but that is really a different side of the coin. And whether such a macro hides a genuine macro definition à la Boost or a recursive template doesn't matter in that respect.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;
}
counting_iterator. You should add that as an answer, or I'll steal it. :)