Template function to display container

A

Alex Vinokur

I would like to define template function to display any container.
Something like (but syntax below is illegal because of S<T>):


template<typename S, typename T>
void display (std::eek:stream& o_stream, const S<T>& i_data, const
std::string& i_delim = " ")
{
o_stream << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<T> (o_stream, i_delim));
o_stream << std::endl << std::flush;
}

Is there any solution?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
Joined
Jan 12, 2008
Messages
4
Reaction score
0
Code:
template< typename CONTAINER > 
void display( const CONTAINER& cntr, std::ostream& stm,
              const std::string& delim )
{
  typedef typename CONTAINER::value_type type ;
  std::copy( cntr.begin(), cntr.end(), 
             std::ostream_iterator<type>( stm, delim.c_str() ) ) ;
  
}
 
K

Kira Yamato

I would like to define template function to display any container.
Something like (but syntax below is illegal because of S<T>):


template<typename S, typename T>
void display (std::eek:stream& o_stream, const S<T>& i_data, const
std::string& i_delim = " ")
{
o_stream << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<T> (o_stream, i_delim));
o_stream << std::endl << std::flush;
}

try

template<template<class T> S, typename T>
 
M

Martin York

Well to match the std library you could make display() use iterators.

But if you really must pass the array then:

#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>

template<typename T>
void display(std::eek:stream& s,T const& i_data,const std::string&
i_delim = " ")
{
s << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<typename T::value_type> (s,i_delim.c_str()));
s << std::endl << std::flush;
}
int main()
{
std::vector<int> x;
std::list<int> y;

display(std::cout,x);
display(std::cout,y);
}
 
A

Alex Vinokur

Well to match the std library you could make display() use iterators.

But if you really must pass the array then:

#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>

template<typename T>
void display(std::eek:stream& s,T const& i_data,const std::string&
i_delim = " ")
{
        s << std::flush;
        std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<typename T::value_type> (s,i_delim.c_str()));
        s << std::endl << std::flush;}

int main()
{
    std::vector<int>    x;
    std::list<int>      y;

    display(std::cout,x);
    display(std::cout,y);

Thanks.

I like this solution.

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
A

Alex Vinokur

Kira Yamato dixit:
template<template<typename T> class S, typename T>

Right.  I was too careless.

Thanks.


void display(std::eek:stream &, const S<T> &, const std::string &)
...
[...]

Thanks.

template<template<typename X> class V, typename S>
void display2 (std::eek:stream& o_stream, const V<S>& i_data, const
std::string& i_delim = " ")
{
o_stream << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<S> (o_stream, i_delim.c_str()));
o_stream << std::endl << std::flush;
}


But

std::vector<std::string> v;

display2(std::cout, v)
produces

error C2784: 'void adj::display2(std::eek:stream &,const V<S> &,const
std::string &)' : could not deduce template argument for 'const V<S>
&' from 'std::vector<_Ty>'
with
[
_Ty=std::string
]

while compiling with Microsoft Visual C++ 2005.


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
J

James Kanze

On 2008-02-13 02:42:07 -0500, pelio <[email protected]> said:
Kira Yamato dixit:
On 2008-02-13 00:41:03 -0500, Alex Vinokur
<[email protected]> said:
I would like to define template function to display any container.
Something like (but syntax below is illegal because of S<T>):
template<typename S, typename T>
void display (std::eek:stream& o_stream, const S<T>& i_data, const
std::string& i_delim = " ")
{
o_stream << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<T> (o_stream, i_delim));
o_stream << std::endl << std::flush;
}
try
template<template<class T> S, typename T>
template<template<typename T> class S, typename T>
Right. I was too careless.
void display(std::eek:stream &, const S<T> &, const std::string &)
...
[...]
template<template<typename X> class V, typename S>
void display2 (std::eek:stream& o_stream, const V<S>& i_data, const
std::string& i_delim = " ")
{
o_stream << std::flush;
std::copy (i_data.begin(), i_data.end(),
std::eek:stream_iterator<S> (o_stream, i_delim.c_str()));
o_stream << std::endl << std::flush;
}

std::vector<std::string> v;
display2(std::cout, v)
produces
error C2784: 'void adj::display2(std::eek:stream &,const V<S> &,const
std::string &)' : could not deduce template argument for 'const V<S>
&' from 'std::vector<_Ty>'
with
[
_Ty=std::string
]
while compiling with Microsoft Visual C++ 2005.

There are two problems with the proposed solution: the first is
that I'm pretty sure that something like C<V> is a
non-deduceable context---the compiler won't even try to figure
out what the actual type is. The second, of course, is that
std::vector et all won't match the template template parameter,
because they have more than one parameters (the allocator, and
in the case of std::basic_string, the traits as well); the fact
that all but the first parameter has a default doesn't change
the "type" of the template.

The STL was designed expressedly so that this sort of thing
isn't necessary. That's why containers are required to have a
typedef value_type, for example. Just use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top