How to extract and modify a "sub-vector"

I have a std::vector<int> and I want to modify subset of the elements. I thought I might be able to use:
1
2
3
4
5
6
7
8
    std::vector<int> a = { 1,2,3,4,5,6,7,8,9 };
    std::vector<int> b(&a[3],&a[7]);
    for(auto& each : b) {
        each++;
    }
    for(auto& each : a) {
        std::cout << each << "\n";
    }

but that didn't work. The elements of 'a' retain their original values. Then, I thought, "Ooo, maybe I could make 'b' a reference." Nope. What approach would You suggest in accessing a subset of a vector for potential alteration?
You could use a vector of reference wrappers

std::vector<std::reference_wrapper<int>> b(a.begin() + 3, a.begin() + 7);
demo: http://coliru.stacked-crooked.com/a/8bad937df64b3780

but a more generic approach is to program in terms of iterator ranges, not in terms of containers.
@Cubbi: Very interesting, thanks. Can You elaborate on what You mean when You say, "program in terms of iterator ranges, not in terms of containers"?
I mean something like
std::for_each(a.begin() + 3, a.begin() + 7, [](int& n){++n;});
http://coliru.stacked-crooked.com/a/5f6abe4ce37c8343

or, more generally, writing a function (possibly template) that takes a pair of iterators
Topic archived. No new replies allowed.