1

我正在尝试使用 stringstreams 将整数转换为字符串。我这样做:

std::string const DilbertImage::startUrl = "http://tjanster.idg.se/dilbertimages/dil";
std::string const DilbertImage::endUrl = ".gif";

 DilbertImage::DilbertImage(int d)
{
    cal.setDate(d);

    int year, month, date;

    year = cal.getYear();
    month = cal.getMonth();
    date = cal.getNumDate();

    std::stringstream ss;

    ss << year << "/";

    if(month < 10)
    {
        ss << 0;
    }

    ss << month << "/" << "Dilbert - " << cal.getNumDate() << ".gif";

    filePath = ss.str();

    ss.str("");
    ss.clear();

    ss << startUrl << date << endUrl;

    url = ss.str();

    std::cout << url << '\t' << filePath << std::endl;
}

我希望得到两个看起来像这样的漂亮字符串:

url: http://tjanster.idg.se/dilbertimages/dil20060720.gif
filePath: /2006/07/Dilbert - 20060720.gif

但是,当我将整数放入字符串流中时,它们以某种方式最终得到空格(或在它们中间插入的其他空白字符)当我从控制台窗口粘贴它时,字符显示为 *.

他们最终看起来像这样:

url: http://tjanster.idg.se/dilbertimages/dil20*060*720.gif 
filepath: /2*006/07/Dilbert - 20*060*720.gif

为什么会这样?

这是整个项目: http: //pastebin.com/20KF2dNL

4

1 回答 1

5

"*"字符是千位分隔符。有人一直在弄乱你的语言环境

这可能会解决它:

std::locale::global(std::locale::classic());

如果您只想覆盖numpunct方面(确定数字的格式):

std::locale::global(std::locale().combine<std::numpunct<char>>(std::locale::classic()));

在您的情况下,当您设置瑞典语言环境时:

std::locale swedish("swedish");
std::locale swedish_with_classic_numpunct = swedish.combine<std::numpunct<char>>(std::locale::classic());
std::locale::global(swedish_with_classic_numpunct);
于 2012-07-11T10:29:42.690 回答