-1

I am very certain that I saw the syntax for what I want to do on this website a while ago, but I cannot find it anymore and I forgot what it was. Say I wanted to store the values from 1 to a 100 in an array. What would be the syntax without using a loop. I remember the syntax being something like this:

int line [] = {1 .. 100};

What is the correct syntax?

UPDATE: I figured out what I meant by this question. What I read a while ago was the syntax to have all the elements in an array equal to a number. For example, in a GCC compiler, you can set all the elements in an array equal to zero like this:

int line [10] = {[0 ... 9] = 0}; 

By doing this, all the elements in the array will be equal to 0. It is a very useful thing to know in my opinion and much easier than using a for-loop.

  • 2
    _"I remember the syntax being something like this"_ That might be a compiler extension, or you have seen some _pseudo code_, but that's not standard c++ IIRC. –  Feb 13 '18 at 19:08
  • 5
    Using [`std::iota`](http://en.cppreference.com/w/cpp/algorithm/iota). – Some programmer dude Feb 13 '18 at 19:09
  • Possible duplicate of [Set std::vector to a range](https://stackoverflow.com/questions/11965732/set-stdvectorint-to-a-range) –  Feb 13 '18 at 19:11

1 Answers1

0

Try like this:

const int line[5] = { 1, 2, 3, 4, 5 };

karol
  • 380
  • 1
  • 5
  • 16