n桁の0埋めの数字を作る

なんて言えばいいのか分からないんだけど、0001とか002とかそういう数字作る時にif文で桁数見たりするのもアレなので書いた。C++勉強してないからもっといい方法ある気はする。

ぐぐらびりてぃが低いので良いタイトル募集します。

char const *digitString(int num, int digit) {
    char c[32];
    sprintf(c, "%d", num);

    std::string s(c);
    while (s.length() < digit) {
        s = "0" + s;
    }
    return s.c_str();
}


こんな感じかな。

std::ostringstream oss;
oss << std::setw(n) << std::setfill('0') << n;
return oss.str().c_str();

C++の基本的な事分からずにcocos2d-x書いてるから入門書とか読む必要を感じた。