2

I'm trying to use a range based for loop to iterate from the beginning of a vector to a variable index x as shown.

void algorithm(vector<int> arr, int n)
{
int count=0;
for (int i:arr[n])
count++;
}

I understand the above function is rather useless but I'm trying to conceptually understand if there is a way to do so. arr[n] gives me an error as does arr.begin()+n

3
  • 1
    Since the vector (presumably arr) is passed by value and changes to it won't be visible to the calluer, simply resize based on n, and then use a range-based loop for (int i: arr)... as usual. Otherwise, it is not possible to use a range-based for as you wish. Commented Oct 30, 2019 at 9:48
  • @Blaze my problem is that doing so will iterate through all of vector arr, my goal is to only iterate up to index n Commented Oct 30, 2019 at 9:48
  • @Risen Why not just for (int i=0; i<n; i++) std::cout << arr[i] << "\n"; ? Commented Oct 30, 2019 at 9:55

4 Answers 4

2

You can increment a counter and break the loop when the desired count is reached.

Something like:

void func(const std::vector<int> & v, std::size_t n)
{
    std::size_t count(0);

    for(int e : v)
    {
        if(count < n)
        {
            std::cout << e << " "; // Do what you want with e.
            ++count;
        }
        else
            break; // Exit the loop
    }
}

But I would not recommend doing it. Range-based for loops are meant to iterate over the whole container.
The above solution is thus a less readable way of simply doing:

void func(const std::vector<int> & v, std::size_t n)
{
    for(std::size_t i = 0; (i < v.size()) && (i < n); ++i)
    {
        std::cout << v[i] << " "; // Do what you want with v[i].
    }
}

Or also:

void func(const std::vector<int> & v, std::size_t n)
{
    std::size_t count(0);
    for(std::vector<int>::const_iterator cit = v.cbegin(); (cit != v.cend()) && (count < n); ++cit, ++count)
    {
        std::cout << *cit << " "; // Do what you want with *cit
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Range-based for loop is pure sugar - it can only iterate over the entire range.

If you need to play with the iterators you have to fall back on a for loop.

Comments

0

According to the documetion the for range loop is translate to :

{

    auto && __range = range_expression ;
    for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) {

        range_declaration = *__begin;
        loop_statement

    }

} 

so you can't set an ending point like you trying because its it a iterator to a place and its not arr.end(). but if you know the ending index you can try doing this:

vector<int> new_vec(arr.begin(), arr,(begin()+index));
for(auto& e:new_vec)
{
//some code
}

Comments

0

Just to add to the previous answers:

While at present this is not possible, C++20 will ship with much more support for ranges. I found this article helpful in understanding some of the proposed features. As far as I understand you will be able to write

void algorithm(vector<int> arr, int n){
   int count=0;
   for (int i: arr | take(n))
      count++;
}

There is no implementation of this proposal yet, so I am not aware of a way to test my code.

Comments

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.