C++ boost::algorithm::join 的一个很好的例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1833447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:19:59  来源:igfitidea点击:

A good example for boost::algorithm::join

c++stringboostjoin

提问by Dan Hook

I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and I didn't want to invest a lot of time learning the Boost Range library just to use this one function.

我最近想使用boost::algorithm::join但我找不到任何使用示例,而且我不想为了使用这个函数而花费大量时间学习 Boost Range 库。

Can anyone provide a good example of how to use join on a container of strings? Thanks.

谁能提供一个关于如何在字符串容器上使用 join 的好例子?谢谢。

回答by Tristram Gr?bener

#include <boost/algorithm/string/join.hpp>
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> list;
    list.push_back("Hello");
    list.push_back("World!");

    std::string joined = boost::algorithm::join(list, ", ");
    std::cout << joined << std::endl;
}

Output:

输出:

Hello, World!

回答by KeatsPeeks

std::vector<std::string> MyStrings;
MyStrings.push_back("Hello");
MyStrings.push_back("World");
std::string result = boost::algorithm::join(MyStrings, ",");

std::cout << result; // prints "Hello,World"