LoginSignup
2
0

More than 3 years have passed since last update.

C++でsprintf的な文字列フォーマットを行う

Posted at

C++には、sprintf的な文字列フォーマットを行う機能は標準では備わっていない。そのため、Boost Format Libraryに含まれるboost::formatを使うなどする必要がある。

Boost Format Libraryを使う場合の例:

// サンプルコード
#include <iostream>
#include <string>
#include <boost/format.hpp>

int main()
{
    const std::string s = (boost::format("%03.4d %s") % 3 % "abc").str();
    std::cout << s << std::endl;
}

実行結果:

003 abc

参考

文字列フォーマット - boostjp : Boost日本語情報サイト
https://boostjp.github.io/tips/format.html

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0