1

I want to iterate over a simple double array using iterators. I wonder how I can do that actually.

What I would like to have is something like this:

void foo(double* array, size_t size)
{
    std::vector<double> real(size);
    std::vector<double> imag(size);
    auto arrBegin = std::begin(array); // this of course doesn't work
    auto arrEnd = std::end(array); // this of course doesn't work

    bool toggle = false;

    std::partition_copy(arrBegin, arrEnd, std::begin(real), std::begin(imag),
                [&toggle] (int) {return toggle != toggle});

    //Do other stuff
}

As some of you might guess, the array is an interleaved complex number, that I would like to split up into arrays of real and imaginary part.

Is there a way to do that using an array that I receive as a pointer if I know the size of the array, or do I need to use a separate class like std::vector for the array in order to work with iterators?

I would appreciate to use std::partition_copy(), but I am open to other ideas.

Note: I cannot use std::array because the array size is not known during compile time. std::vector would be possible, but I actually want to see whether it's possible to do it without std::vector as I receive the array from an API and want to use it as it is.

3
  • That's why you should use std::array. Commented Apr 12, 2016 at 8:57
  • Note that your vectors aren't big enough to hold the output ranges. Commented Apr 12, 2016 at 8:57
  • 1
    On an unrelated note, if you have a size use size_t instead. On a 64-bit system it's most likely an alias of an unsigned 64-bit integer type just like uint64_t, but it conveys more semantical information to the reader of the code. Commented Apr 12, 2016 at 9:00

1 Answer 1

2

Use array and array + size as your iterators.

You probably also want to use inserter-iterators for the vectors.

#include <algorithm>
#include <iterator>

std::partition_copy(array, array + size,
                    std::back_inserter(real),
                    std::back_inserter(imag),
                    [&toggle](int t){ return t != toggle; });
Sign up to request clarification or add additional context in comments.

8 Comments

This answer requires some explanations. What are you doing, this is pretty advanced.
@Chiel: Which part of this is unclear and not covered by the respective documentations?
Why the back inserters? Why the modification to the lambda function? Your solution looks elegant if only I understood it.
@Chiel: Inserters because you want to insert (check the documentation of partition_copy. The OP's lambda code was ill-formed. I'm assuming that the OP knows how the algorithms work; the question gave me no reason to believe otherwise.
Thanks man. Works like a charm. Btw: are the back_inserters in any way preferable to std::begin / std::end if I preallocate the vectors? Oh and my lambda function actually works, as I have shown it above, What did I do wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.