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

标签 c++ string

<分区>

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

如何在 C++ 中将整数值转换为 string

这是我试过的:

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

这里是错误:

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

最佳答案

您可以使用字符串流:

#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
}

关于c++ - size_t 在 C++ 中转换/转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510077/

相关文章:

c++ - 冒泡排序优化 C++

c++ - 如何将 python 类包装到 C++ 类中?

string - 删除R中字符串中位置的字符?

c++ - 查找全局定义的指针指向已使用堆分配的地址

C++ 线程 : cannot unlock mutex in array after condition_variable wait

c++ - while 循环接受第一个和第二个 cin 的输入

string - 使用 Excel-VBA 从字符串中提取 5 位数字

r - 仅在顶层拆分带有嵌套括号的字符串,其中 "level"由括号确定

python - 根据标点符号或驼峰式大小写分割句子

java - 检查字符串是否匹配特定的正则表达式