Re: How to pass STL containers (say a vector) ?
Daniel T. skrev:
In article <1148070525.301764.67280@u72g2000cwu.googlegroups.com>,
"peter koch" <peter.koch.larsen@gmail.com> wrote:
Daniel T. skrev:
In article <1148063503.729070.325510@j33g2000cwa.googlegroups.com>,
[snip]
I beg to differ, std::copy does return a container in its own way...
Well... I just notice the line above. I agree that std::copy might make
the data copied available somehow. The difference is one of words. I
meant return as used in a C++ program whereas you seemingly mean return
in the sense that the data will afterwards be available to the caller.
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.
What about transform?
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.
How so? All the data collected inside the copy function is given to the
caller through foo...
Surely. But std::copy did not return the data - it simply put them into
some iterator.
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.
The same way std::remove does it.
That does not remove the data from the container.
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(???)));
template < typename FwIt > void normalize( FwIt first, FwIt last );
template < typename FwIt > void print( FwIt first, FwIt last ) {
copy( first, last, ostream_iterator<int>( cout, " " ) );
}
normalize( vec.begin(), vec.end() );
print( vec.begin(), vec.end() );
You get the same functionality but with an added complexity:
std::vector<int> vec;
func(vec);
normalize( vec.begin(), vec.end() );
print( vec.begin(), vec.end() );
(forgetting that vec is still in scope).
Versus:
print(normalise(func()));
One simple line. Efficient, leaves no mess.
/Peter