Skip to content

Instantly share code, notes, and snippets.

@nukopy
Created January 2, 2019 07:58
Show Gist options
  • Save nukopy/03ef1024fe38f361e9e380d15c6e0fa6 to your computer and use it in GitHub Desktop.
Save nukopy/03ef1024fe38f361e9e380d15c6e0fa6 to your computer and use it in GitHub Desktop.
Padding with STL, "ios", "iomanip" on C++.
// 左埋めパディング
// 必用なヘッダーインクルード
#include <ios> // std::left, std::right
#include <iomanip> // std::setw(int), std::setfill(char)
// macro 直後の要素をパディング
#define zero_pad(num) setfill('0') << std::right << setw(num)
#define space_pad(num) setfill(' ') << std::right << setw(num)
// std::left とすれば,右に '0' を埋めることができる
int main() {
// input
int a = 33, b = 5555, c = 67891;
// calculation
cout << zero_pad(0) << a << "\n";
cout << zero_pad(2) << a << "\n";
cout << zero_pad(4) << a << "\n";
cout << zero_pad(4) << b << "\n";
cout << zero_pad(6) << b << "\n";
cout << zero_pad(10) << b << "\n";
cout << zero_pad(3) << b << "\n";
cout << zero_pad(5) << b << "\n";
cout << zero_pad(10) << b << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment