Re: How to pass STL containers (say a vector) ?
Daniel T. skrev:
In article <1148063503.729070.325510@j33g2000cwa.googlegroups.com>,
"peter koch" <peter.koch.larsen@gmail.com> wrote:
Daniel T. skrev:
In article <Wahbg.228$D_5.37@fe12.lga>,
Sanjay Kumar <nospam-hypertree@yahoo.com> wrote:
Folks,
I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?
The same way the standard algorithms do. 'std::copy' for example accepts
a container as an input param, and another container as an output param.
Well.... that one is a bad example. std::copy does not return a
collection. Actually, i can't remember a single std::algorithm that
does so (but I am tired and might well be wrong).
In my opinion you need very strong arguments (and those arguments
include measured improvements) in order to not return by value.
I beg to differ, std::copy does return a container in its own way...
vector<int> foo;
copy( istream_iterator<int>( cin ), istream_iterator<int>(),
back_inserter( foo ) );
The data in foo was returned...
So you mean that the data was returned in foo? In that case we simply
have a different perception of "returning values". To me, std::copy
does not return data in foo.
Perhaps this example better demonstrates what i mean?
vector<int> foo;
foo.push_back(117);
copy( istream_iterator<int>( cin ), istream_iterator<int>(),
back_inserter( foo ) );
copy definitely does not return its data in foo.
In other words when you want to pass in a container:
tempalte < typename InIt >
void func( InIt first, InIt last );
Fine! And now let func remove the second element.
when you want to return a container:
template < typename OutIt >
void func( OutIt first );
It still does not return a container.
template <class container> void normalise_container(container const&
c);
template <class container> void print_container(container const& c);
print_container(normalise_container(func(???)));
/Peter