This is more like a follow-up question to Is it possible to send part of vector as a vector to a function? but it's different and I couldn't ask it there.
I include the specific answer that is interesting in my case:
Use iterators as the parameters to the range-based function, and pass in the required range. Your code in the function becomes
funcWithRange(v.cbegin()+10, v.cbegin()+50); with function signature
void funcWithRange(std::vector::const_iterator first, std::vector::const_iterator last) This could be generalized by making this a function template with the vector member type as its template parameter, or still further to any container supporting this type of range iteration. As noted in the comments, has many examples of this pattern.
std::distance(first, last); will return the desired altered size. I don't think you can get closer to meeting your requirements without making a physical copy.
This is all fine but I wonder about how to "use" the vector (my vector is called "numbers" in the new function. As my program look now I just use numbers[i], numbers[i + 1] and numbers.size(). I don't know how to do when I have two in parameters. I guess I can use first + ? or something but since it is a recursive function I would like to be able to treat is just as a vector.
My second question is how do I write the function signature when I have using namespace std;? Normally I just cut off everything before the :: and the :: but this time I see them twice in both first and last.
I'm sorry about asking this here when it's so similar to other questions already answered. I first tried to send an e-mail to the person posting the answer and he recommended me to post it here.
Update:
Thank you for answers still I can't really see how I can use this. Not because your answers weren't good but because I didn't describe it good enough. Ishould also attach the code with the function:
bool deeperLook (vector<int> numbers, int target)
{
for (int i = 0 ; i < numbers.size() ; i++)
{
if (numbers[i] == target)
{
return true;
}
if (numbers[i] < target)
{
deeperLook({numbers.cbegin() + i, numbers.cbegin() + numbers.size()} , target - numbers[i]);
}
while(numbers[i] == numbers[i+1])
{
i++;
}
}
return false;
}
This function will get a sorted vector of int (biggest first) and then try to create a combination of the numbers that added together reach a specific number (target). bool deeperLook (vector numbers, int target) As you see, the function wants a vector as indata. That vector is sent to another part of the program and also, it's recursive so I need to send it a vector as indata. How do I do this with iterators? The code works fine but it's memory inefficient to create all thoose since it's really just all part of the same vector.
someone also said that I should avoid "using namespace std;". Why? It seems a lot easier to write code with it.