Connecting Tech Pros Worldwide Forums | Help | Site Map

Printing Containers

Paulo Matos
Guest
 
Posts: n/a
#1: Dec 8 '06
Hi all,

Most of the time for debug purposes I wish to print a list, or a vector
or a set of things which I know that have operator<< defined. It might
be list<intbut it might as well be list<FooBar*where FooBar is a
user-defined class.
So I tried to define a template function which would output a container
with T& or T*. It doesn't work:
template <typename T>
std::ostream& LogDebug(const std::string& n, std::const_iterator<T&>&
b, std::const_iterator<T&>& e) {
for(int i = 0, std::const_iterator it = b; it != e; ++it)
std::cerr << "***" << i++ << ": " << *it << "\n";

return std::cerr;
}

template <typename T>
std::ostream& LogDebug(const std::string& n, std::const_iterator<T*>&
b, std::const_iterator<T*>& e) {
for(int i = 0, std::forward_iterator it = b; it != e; ++it)
std::cerr << "***" << i++ << ": " << **it << "\n";

return std::cerr;
}

I think there's a solution. I just can't find it. Any ideas on how to
work this out?

Regards,

Paulo Matos


Greg Buchholz
Guest
 
Posts: n/a
#2: Dec 8 '06

re: Printing Containers



Paulo Matos wrote:
Quote:
Hi all,
>
Most of the time for debug purposes I wish to print a list, or a vector
or a set of things which I know that have operator<< defined. It might
be list<intbut it might as well be list<FooBar*where FooBar is a
user-defined class.
So I tried to define a template function which would output a container
with T& or T*. It doesn't work:
....snip...
Quote:
>
I think there's a solution. I just can't find it. Any ideas on how to
work this out?
template <typename T>
std::ostream& LogDebug(const std::string& n,
typename T::const_iterator it,
typename T::const_iterator e)
{
for(int i = 0; it != e; ++it)
std::cerr << "***" << i++ << ": " << *it << "\n";

return std::cerr;

}

Paulo Matos
Guest
 
Posts: n/a
#3: Dec 8 '06

re: Printing Containers



Greg Buchholz escreveu:
Quote:
>
template <typename T>
std::ostream& LogDebug(const std::string& n,
typename T::const_iterator it,
typename T::const_iterator e)
{
for(int i = 0; it != e; ++it)
std::cerr << "***" << i++ << ": " << *it << "\n";
>
return std::cerr;
>
}
In this case, it would if it is list<int>, what about if it is a
list<int*>? How can it be one?
Is there a way to generalize LogDebug or I would have to define
LogDebugPtr and have **it instead of *it in the output line?

Regards,

Paulo Matos

mlimber
Guest
 
Posts: n/a
#4: Dec 8 '06

re: Printing Containers


Paulo Matos wrote:
Quote:
Hi all,
>
Most of the time for debug purposes I wish to print a list, or a vector
or a set of things which I know that have operator<< defined. It might
be list<intbut it might as well be list<FooBar*where FooBar is a
user-defined class.
So I tried to define a template function which would output a container
with T& or T*. It doesn't work:
template <typename T>
std::ostream& LogDebug(const std::string& n, std::const_iterator<T&>&
b, std::const_iterator<T&>& e) {
for(int i = 0, std::const_iterator it = b; it != e; ++it)
std::cerr << "***" << i++ << ": " << *it << "\n";
>
return std::cerr;
}
>
template <typename T>
std::ostream& LogDebug(const std::string& n, std::const_iterator<T*>&
b, std::const_iterator<T*>& e) {
for(int i = 0, std::forward_iterator it = b; it != e; ++it)
std::cerr << "***" << i++ << ": " << **it << "\n";
>
return std::cerr;
}
>
I think there's a solution. I just can't find it. Any ideas on how to
work this out?
Don't forget the quick and dirty:

class T { /*...*/ }; // Example class
ostream& operator<<( ostream&, const T& ); // defined somewhere

std::vector<Tc; // or std::list<Tor whatever
// ... populate c here ...
std::copy( c.begin(), c.end(),
std::ostream_iterator<T>( std::cerr, "\n" ) );

Cheers! --M

Greg Buchholz
Guest
 
Posts: n/a
#5: Dec 8 '06

re: Printing Containers


Paulo Matos wrote:
Quote:
>
In this case, it would if it is list<int>, what about if it is a
list<int*>? How can it be one?
Is there a way to generalize LogDebug or I would have to define
LogDebugPtr and have **it instead of *it in the output line?

#include<iostream>
#include<vector>
#include<iterator>
#include<string>

template <typename T>
std::ostream& LogDebug(const std::string& n,
typename T::const_iterator it,
typename T::const_iterator e)
{
for(int i = 0; it != e; ++it)
std::cerr << "***" << i++ << ": " << *it << "\n";

return std::cerr;
}
template <template<classclass T, typename U>
std::ostream& LogDebug(const std::string& n,
typename T<U*>::const_iterator it,
typename T<U*>::const_iterator e)
{
for(int i = 0; it != e; ++it)
std::cerr << "***" << i++ << ": " << **it << "\n";

return std::cerr;
}

int main(int argc, char* argv[])
{
int a=42, b=1, c=14;

std::vector<intfoo;
foo.push_back(a); foo.push_back(b); foo.push_back(c);
LogDebug<std::vector<int("",foo.begin(),foo.end()) ;

std::vector<int*bar;
bar.push_back(&a); bar.push_back(&b); bar.push_back(&c);
LogDebug<std::vector, int>("",bar.begin(),bar.end());

return 0;
}

Closed Thread