且听下回分说 2020-03-14 11:11 采纳率: 70%
浏览 1372
已采纳

c++ 中将list中的元素合并,转换成string类型

c++ 中将list中的元素合并,转换成string类型

实现函数 “string ListToString(const std::list& lstValue)”

将list中的元素合并,转换成string类型;

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-03-14 12:00
    关注
    // Q1058622.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    #include <sstream>
    #include <memory>
    #include <string>
    #include <list>
    
    using namespace std;
    
    string ListToString(const std::list<int>& lstValue)
    {
        if (lstValue.empty()) return "";
        std::ostringstream ss;
        int i = 0;
        for (auto it = lstValue.cbegin(); it != lstValue.cend(); it++, i = 1)
        {
            if (i != 0) ss << ",";
            ss << *it;
        }
        return ss.str();
    }
    
    int main()
    {
        std::list<int> lstValue;
        lstValue.push_back(1);
        lstValue.push_back(2);
        lstValue.push_back(3);
        string s = ListToString(lstValue);
        cout << s << endl;
        return 0;
    }
    
    

    1,2,3

    问题解决的话,请点下采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型