473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Printing Containers

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

Dec 8 '06 #1
4 1198

Paulo Matos wrote:
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...
>
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;

}

Dec 8 '06 #2
Paulo Matos wrote:
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

Dec 8 '06 #3

Greg Buchholz escreveu:
>
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

Dec 8 '06 #4
Paulo Matos wrote:
>
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;
}

Dec 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Dylan | last post by:
I'd like to compare two containers. They should be considered equivalent if both containers have the same number of elements with the same values, no matter what order the values are in. For...
14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
1
by: Jean-Marc Blaise | last post by:
IBM recommends to place each ts container on a different physical disk. What about the impacts if all containers (DMS) are in the same FS (supported by multiple disks) ? Does DB2 preallocate...
2
by: Rodney Dunning | last post by:
Forgive me if this is a stupid question. I have a course syllabus located at http://www.longwood.edu/staff/dunningrb/teaching/easc101f06/easc101f06_syllabus.html. I use a <divcontainer for...
4
by: Michel Esber | last post by:
Hello, DB2 V8 FP12 LUW. My system is under heavy wait-io times. I have read the following article: http://tinyurl.com/p844z and it says that prefetchsize should be the product of extentsize...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
11
by: jimxoch | last post by:
Hi list, Most STL containers are storing their data on the heap. (although some std::string implementations are notable exceptions) Of course, using the heap as storage increases flexibility and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.