format


1. Formatting Strings with Formatting Flags

#include <iostream>

using namespace std;

int main() {
  int age = 30;
  string name = "John";
  
  // Left-align with 10 characters
  cout << left << setw(10) << name << endl;
  
  // Right-align with 10 characters
  cout << right << setw(10) << age << endl;
  
  // Zero-pad with 10 characters
  cout << setfill('0') << setw(10) << age << endl;
  
  // Show + sign for positive numbers
  cout << showpos << age << endl;
  
  // Show minus sign for negative numbers
  cout << noshowpos << -age << endl;
  
  return 0;
}

2. Formatting Strings with Formatting Specifiers

3. Formatting Dates and Times

4. Formatting Input and Output to Files

5. Formatting Strings Using Boost.Format

6. Formatting Strings Using fmtlib

7. Formatting Numbers as Currency

8. Formatting Numbers as Percentages

9. Formatting Strings with std::stringstream

10. Formatting Strings with std::to_string

11. Formatting Strings with Boost.LexicalCast