Skip to main content
added 574 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

If you are not sure what iterators are then think of pointers (they don't have to be pointers but that is an easy way to visualize them for a beginner). The begin points at the first element in the container, and the end points one past the end of the container.

So you can loop like this:

 for (I loop = begin; loop != end; ++loop) {
      // Do Stuff
      auto const& val = *loop; // read the value.
      (*loop)         = XXX;   // write the value.
 }

Also this function can be replaced by a one line expression by using std::generate(): or std::iota

Also this function can be replaced by a one line expression by using std::generate():

If you are not sure what iterators are then think of pointers (they don't have to be pointers but that is an easy way to visualize them for a beginner). The begin points at the first element in the container, and the end points one past the end of the container.

So you can loop like this:

 for (I loop = begin; loop != end; ++loop) {
      // Do Stuff
      auto const& val = *loop; // read the value.
      (*loop)         = XXX;   // write the value.
 }

Also this function can be replaced by a one line expression by using std::generate() or std::iota

added 43 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
void prime_sequence(unsigned int *data)
{
    // fill data with values 0 to SEQUENCE_LENGTH-1 in
    for (unsigned int i = 0; i < SEQUENCE_LENGTH; i++)
    {
        data[i] = i;
    }
}

// ----

std::generate(begin, end, [][t = 0]() mutable {staticreturn intt++;}); count// =requires 0;C++14 returnor count++;}up
// or a better solution suggested by @Rish
std::iota(begin, end, 0);
void prime_sequence(unsigned int *data)
{
    // fill data with values 0 to SEQUENCE_LENGTH-1 in
    for (unsigned int i = 0; i < SEQUENCE_LENGTH; i++)
    {
        data[i] = i;
    }
}

// ----

std::generate(begin, end, [](){static int count = 0; return count++;});
void prime_sequence(unsigned int *data)
{
    // fill data with values 0 to SEQUENCE_LENGTH-1 in
    for (unsigned int i = 0; i < SEQUENCE_LENGTH; i++)
    {
        data[i] = i;
    }
}

// ----

std::generate(begin, end, [t = 0]() mutable {return t++;}); // requires C++14 or up
// or a better solution suggested by @Rish
std::iota(begin, end, 0);
Fixed typo.
Source Link
Madagascar
  • 10.1k
  • 1
  • 16
  • 52
int main()
{

    // OK so you don't want random numbers.
    // Which is fine. But basically this means
    // you get exactly the same results from rand()
    // everytime you run this application.
    srand(SEED);



    // Sure arrays in C.
    // When you talk about arrays in C most people think about
    // dynamically allocated arrays. But these are fine.
    unsigned int data[SEQUENCE_LENGTH] = {0};


    // What I would note is that there is a limit to
    // SEQUENCE_LENGTH before it becomes to large.
    // So in this situation `std::vector` is superior as this
    // limit is much larger.
    // https://stackoverflow.com/a/216731/14065
    unsigned int max_history[SEQUENCE_LENGTH] = {0};





    // Fine:
    prime_sequence(data);
    // using the template version woulewould be:
    prime_sequence(data, data + SEQUENCE_LENGTH);
    // Though I would use:
    prime_sequence(std::begin(data), std::end(data));



    // Don't use `std::endl`.
    // It forces a flush when it is not needed.
    // Simply use `"\n"`.
    std::cout << "max_sum: " << max_sum << " cum_sum: " << cum_sum << std::endl;


    // No point.
    // If your application can only return `0` then
    // not having a value is an indication that your application will
    // never fail.
    return 0;
}
int main()
{

    // OK so you don't want random numbers.
    // Which is fine. But basically this means
    // you get exactly the same results from rand()
    // everytime you run this application.
    srand(SEED);



    // Sure arrays in C.
    // When you talk about arrays in C most people think about
    // dynamically allocated arrays. But these are fine.
    unsigned int data[SEQUENCE_LENGTH] = {0};


    // What I would note is that there is a limit to
    // SEQUENCE_LENGTH before it becomes to large.
    // So in this situation `std::vector` is superior as this
    // limit is much larger.
    // https://stackoverflow.com/a/216731/14065
    unsigned int max_history[SEQUENCE_LENGTH] = {0};





    // Fine:
    prime_sequence(data);
    // using the template version woule be:
    prime_sequence(data, data + SEQUENCE_LENGTH);
    // Though I would use:
    prime_sequence(std::begin(data), std::end(data));



    // Don't use `std::endl`.
    // It forces a flush when it is not needed.
    // Simply use `"\n"`.
    std::cout << "max_sum: " << max_sum << " cum_sum: " << cum_sum << std::endl;


    // No point.
    // If your application can only return `0` then
    // not having a value is an indication that your application will
    // never fail.
    return 0;
}
int main()
{

    // OK so you don't want random numbers.
    // Which is fine. But basically this means
    // you get exactly the same results from rand()
    // everytime you run this application.
    srand(SEED);



    // Sure arrays in C.
    // When you talk about arrays in C most people think about
    // dynamically allocated arrays. But these are fine.
    unsigned int data[SEQUENCE_LENGTH] = {0};


    // What I would note is that there is a limit to
    // SEQUENCE_LENGTH before it becomes to large.
    // So in this situation `std::vector` is superior as this
    // limit is much larger.
    // https://stackoverflow.com/a/216731/14065
    unsigned int max_history[SEQUENCE_LENGTH] = {0};





    // Fine:
    prime_sequence(data);
    // using the template version would be:
    prime_sequence(data, data + SEQUENCE_LENGTH);
    // Though I would use:
    prime_sequence(std::begin(data), std::end(data));



    // Don't use `std::endl`.
    // It forces a flush when it is not needed.
    // Simply use `"\n"`.
    std::cout << "max_sum: " << max_sum << " cum_sum: " << cum_sum << std::endl;


    // No point.
    // If your application can only return `0` then
    // not having a value is an indication that your application will
    // never fail.
    return 0;
}
added 297 characters in body
Source Link
G. Sliepen
  • 69.3k
  • 3
  • 75
  • 180
Loading
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading