我正在尝试使用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

最佳答案

"*"字符是thousands separator。有人在弄乱您的locale

这可能会解决:

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);

关于c++ - 将int放入字符串流时,它会插入奇怪的字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11430442/

10-11 22:38
查看更多