菜鸟笔记
提升您的技术认知

C++中std::vector转string

在C++中将std::vector在不经过for遍历的前提下转为string

  1. accumulate函数
#include <vector>
#include <string>
#include <numeric> //accumulate需要
#include <iostream>
int main()
{
    std::string str;
    std::vector<std::string> list;
    list.push_back("hello");
    list.push_back("world");
    str = accumulate(list.begin(), list.end(), str);
    std::cout<<str.c_str()<<std::endl;
}

// 输出
helloworld