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

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

报告相同问题?