Function pointers versus calling cout directly

I am working on the assignment of a C ++ class, and I need some input. I'm not looking for “help” with my purpose, as I use class assignments as an excuse to practice writing “good” code, and I discuss between the two solutions to the current problem I'm working on.

I store approximately 4,000 values ​​on an STL map. One of the features of this program is to print each value on my map. I have a class called "driver" that processes the internal logic of the application and contains a link to the map. I have a separate class for the user interface.

What I was going to do was pass the function pointer to my driver, which iterates over the map and returns to the function in my user interface class. Would it be a bad idea if there were approximately 4,000 function calls? Should I bite a bullet and just call cout during iteration? I hesitate to do this because I am trying to completely isolate my user interface from my logic and program data. I know, in the end, it really does not matter, since it is just the purpose of the class, but hypothetically assuming that this is the code that I would like to save in the end, what would be the "best" practice here?

+3
source share
2 answers

: . 4k - . , :

  • code it the easy way
  • ? .
  • ? , gprof, , , ,

, " - ".

:. :

template <typename IteratorType>
void my_multi_callback(iterator start, iterator end);

map ( ). , .

+6

, , . cout, ostream_iterator. , () , ( , , back_inserter).

, , () . , , , ( , ), / , , .

+2

Source: https://habr.com/ru/post/1796219/


All Articles