vector - overloading the << and defining a stream manipulator c++ -


i hoping offer insight on particular problem im having. writing program takes in integers, stores them in vector, , prints them out comma separators numbers larger 999 -> 1,000.

my question is.. well, 2 actually, how can pass vector function, , second, if wanted overload << behind scenes possible?

global function class comma:

template <class t> string formatwithcomma(t value){     stringstream ss;     locale commaloc(locale(), new commanumpunc() );     ss.imbue(commaloc);     ss << value;     return ss.str(); 

loop in main() display vector:

for (vector<unsigned int>::iterator = integers.begin(); != integers.end(); ++i){     cout << formatwithcomma(*i) << "  "; } 

first question:

how can pass vector function

just pass directly. example(assume function template):

template <typename t> void processvector(const vector<t>& vec ); 

inside main, can call follows:

processvector<unsigned int> (integers); //an example instantiation 

second question:

if wanted overload << behind scenes possible?

yes, of course possible. see how overload << operator resources: msdn overload << operator , overload << operator wisc

and bunch of resource so: how overload << operator


Comments