From: Hicham Mouline on
Hello,

I have in input an STL container say std::vector<T> or std::list<T> called
my_container;

Is there any advised style re filtering such a container, so that following
code can work only on a subset of that container , without knowing that it
has been filtered?

Say the initial container has 100 elements sorted according to some
criteria, and the filter tells us to work only on the 20 elements.

The filter could return to us a begin iterator and an end iterator to those
20 elements only.
Subsequent code would then use only the iterators, and not functions like
my_container.front()
or
my_container.size()

Is this a customary way? Or is there something that returns a view on the
original container?

regards,



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thiago A. on
> I have in input an STL container say std::vector<T> or std::list<T> called
> my_container;
>
> Is there any advised style re filtering such a container, so that following
> code can work only on a subset of that container , without knowing that it
> has been filtered?
>
> Say the initial container has 100 elements sorted according to some
> criteria, and the filter tells us to work only on the 20 elements.
>
> The filter could return to us a begin iterator and an end iterator to those
> 20 elements only.
> Subsequent code would then use only the iterators, and not functions like
> my_container.front()
> or
> my_container.size()
>
> Is this a customary way? Or is there something that returns a view on the
> original container?

Hi,
I did something similar to what you want:

http://www.thradams.com/codeblog/funlamdas.htm

It is using C++ 0x however can be implemented in C++ 03 using an
abstract interface IEnumerable to replace "auto" and traditional
functors instead lambdas.
Maybe a typedef Filter::Enumerable can replace the IEnumerable
interface for performance as well.

Well, I think it can be implemented in many ways, but the central idea
is to have a lazy evaluation keeping the original container iterator.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel T. on
In article <4b97d862$0$284$14726298(a)news.sunsite.dk>,
"Hicham Mouline" <hicham(a)mouline.org> wrote:

> Hello,
>
> I have in input an STL container say std::vector<T> or std::list<T> called
> my_container;
>
> Is there any advised style re filtering such a container, so that following
> code can work only on a subset of that container , without knowing that it
> has been filtered?
>
> Say the initial container has 100 elements sorted according to some
> criteria, and the filter tells us to work only on the 20 elements.
>
> The filter could return to us a begin iterator and an end iterator to those
> 20 elements only.
> Subsequent code would then use only the iterators, and not functions like
> my_container.front()
> or
> my_container.size()
>
> Is this a customary way? Or is there something that returns a view on the
> original container?

A function like this might be useful to you:

template < typename C >
pair<typename C::iterator, typename C::iterator>
partial_container(C& container, int count)
{
pair<typename C::iterator, typename C::iterator> result;
result.first = container.begin();
result.second = container.begin();
advance(result.second, count);
return result;
}

// sample usage

struct gen
{
int i;
gen(): i(0) { }
int operator()() { return i++; }
};

int main()
{
typedef vector<int> vec_t;
vec_t foo(100);
generate(foo.begin(), foo.end(), gen());

pair<vec_t::iterator, vec_t::iterator> its =
partial_container(foo, 20);
copy(its.first, its.second, ostream_iterator<int>(cout, " "));
cout << '\n';
}

However, if your container is sorted and you want all the elements that
satisfy a particular sort criteria, then upper_bound would be a better
to choice.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 10 mar, 19:15, "Hicham Mouline" <hic...(a)mouline.org> wrote:
> Hello,
>
> I have in input an STL container say std::vector<T> or std::list<T> called
> my_container;
>
> Is there any advised style re filtering such a container, so that following
> code can work only on a subset of that container , without knowing that it
> has been filtered?

Using the range abstraction, along with a range adaptor.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: riap on

> Say the initial container has 100 elements sorted according to some
> criteria, and the filter tells us to work only on the 20 elements.
>
> The filter could return to us a begin iterator and an end iterator to those
> 20 elements only.
> Subsequent code would then use only the iterators, and not functions like
> my_container.front()
> or
> my_container.size()
>
> Is this a customary way? Or is there something that returns a view on the
> original container?
>

If there is no significance to the ordering of the elements of
my_container, then you might consider std::partition (http://
www.sgi.com/tech/stl/partition.html). This would do a partial sort so
that all elements of my_container which pass the filter came at the
start of my_container and return an iterator to the end of the
filtered sequence. I have done something similar to the following in
the past:

container_type::iterator itFilteredEnd =
std::partition(my_container.begin(), my_container.end(), my_filter);
doStuff(my_container.begin(), itFilteredEnd);

Regards


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]