Dereferencing Items in OstreamIterator

I have a collection of pointers, and am wanting to use an ostream_iterator to print the contents of the collection out. Since they are pointers, I only get the mem address. Is there a quick way to de-reference the pointers? Perhaps I should overload the << operator to handle pointers of my type? Thanks.
Last edited on
The addresses *are* the contents of the collection.

You could use an indirection iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <boost/iterator/indirect_iterator.hpp>

int main()
{
    std::vector<std::string*> v = {new std::string("abc"), new std::string("def"), new std::string("bar")};
    
    std::copy(boost::make_indirect_iterator(v.begin()),
              boost::make_indirect_iterator(v.end()),
             std::ostream_iterator<std::string>(std::cout, "\n"));

   for(auto p: v)
      delete p;
}
demo: http://liveworkspace.org/code/4sj5S8

or boost.range adaptor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/copy.hpp>

int main()
{
    std::vector<std::string*> v = {new std::string("abc"), new std::string("def"), new std::string("bar")};
    
    copy(v | boost::adaptors::indirected, 
         std::ostream_iterator<std::string>(std::cout, "\n"));

   for(auto p: v)
      delete p;
}

demo: http://liveworkspace.org/code/VUMQS
Last edited on
Or just:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>

int main()
{
    const int a[] { 0, 1, 2, 3, 4 } ;
    std::vector< const int* > pointers { a, a+1, a+2, a+3, a+4 } ;

    std::transform( pointers.begin(), pointers.end(),
                      std::ostream_iterator<int>( std::cout, " "),
                      [] ( const int* p ) { return *p ; } ) ;

    for( auto ptr : pointers ) std::cout << *ptr << ' ' ;
}
I'm getting a "no = operator" error. From the output it seems I need to overload the iterator's equals?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(void)
{
	TNode first(Vertex(1,1,1)), second(Vertex(2,2,2)), third(Vertex(3,3,3));
	TNode fourth = first;  // No error here
	TNode *fP = &first, *sP = &second, *tP = &third, *lP = &fourth;

	std::list<TNode*> l;
	l.push_back(fP); l.push_back(sP); l.push_back(tP); l.push_back(lP);

	std::ostream_iterator<TNode> out_iter(std::cout, "\n");

	//Error here
	std::transform(l.begin(), l.end(), out_iter, [](const TNode* p)->TNode{return *p;});

	std::copy(l.begin(), l.end(), out_iter);
	std::cin.get();


	return 0;
}


VS2010 doesn't seem to like for (auto x: y) either
Last edited on
With std::ostream_iterator<TNode> out_iter(std::cout, "\n");

There is an obvious error on line 15:
std::copy(l.begin(), l.end(), out_iter);

Line 13 would be an error only if the type TNode is not copyable or not output-streamable.
std::transform(l.begin(), l.end(), out_iter, [](const TNode* p)->TNode{return *p;});
There is an obvious error on line 15:

Yes, you are correct. Didn't see that. Line 15 commented out at least compiles.

Wow, it even prints to the screen :)

Thanks
Topic archived. No new replies allowed.