c++ - Using ostream_iterator on vector of pointers -


here code:

std::vector<int*> osd; osd.push_back(new int(2)); osd.push_back(new int(3));  std::ostream_iterator<int*, char> out_iter2(std::cout, " " ); copy(osd.begin(),osd.end(), out_iter2); 

output: 0x8e6388 0x8e6a8

how make iterator print actual values? need specialize it?

i made specialized operator still doesn't work

std::ostream& operator<<(std::ostream& os, const std::vector<int*>::iterator pd) {      return os << *pd << std::endl;  } 

you can use std::transform lambda expression, gets actual value pointer, such as:

std::ostream_iterator<int, char> out_iter2(std::cout, " " ); std::transform(osd.begin(),                 osd.end(),                 out_iter2,                 [] (int* x) { return *x; }               ); 

demo

edit

here's possible implementation image above link:

template<class inputit, class outputit, class unaryoperation> outputit transform(inputit first1, inputit last1, outputit d_first,                     unaryoperation unary_op) {     while (first1 != last1) {         *d_first++ = unary_op(*first1++);     }     return d_first; } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -