size_t 在 C++ 中转换/转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10510077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:09:38  来源:igfitidea点击:

size_t convert/cast to string in C++

c++string

提问by user881703

Possible Duplicate:
How to convert a number to string and vice versa in C++

可能的重复:
如何在 C++ 中将数字转换为字符串,反之亦然

How do I convert an integral value to a stringin C++?

如何将整数值转换为stringC++ 中的 a?

This is what I've tried:

这是我尝试过的:

for(size_t i = 0;vecServiceList.size()>i;i++)
{
    if ( ! vecServiceList[i].empty() )
    {
        string sService = "\t" + i +". "+  vecServiceList[i] ;
    }
}

And here is the error:

这是错误:

invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+'

回答by dreamlax

You could use a string stream:

您可以使用字符串流:

#include <sstream>

...


for (size_t i = 0; ... ; ...)
{
    std::stringstream ss;

    ss << "\t" << i << vecServiceList[i];

    std::string sService = ss.str();

    // do what you want with sService
}