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

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?