SHARE
TWEET

Basic printf-a-like

ulfben Jan 5th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. namespace utils{
  3.     void printf(const char* format){
  4.         std::cout << format;
  5.     }
  6.     template<typename T, typename ... Args>
  7.     void printf(const char* format, T value, Args ... args){
  8.         for ( ; *format != '\0'; format++ ) {
  9.             if ( *format == '%' ) {
  10.                std::cout << value;
  11.                printf(format + 1, args ... );
  12.                return;
  13.             }
  14.             std::cout << *format;
  15.         }
  16.     }
  17.     template<typename T, typename ... Args>
  18.     void printf(const std::string& format, T value, Args ... args){
  19.         printf(format.c_str(), value, args...);
  20.     }
  21. }
  22.  
  23. int main(){
  24.     utils::printf('% world% %\n', "Hello", '!', 2019);                            
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top