c++ - 将范围之间的字符串 vector 转换为分隔字符串(允许 boost )

标签 c++ boost

我有一个大小为 10 的字符串 vector 。我想将它连接到一个从索引 3 到 6 的字符串,并以空格作为分隔符。我知道 boost::algorithm::join 可以对整个 vector 执行此操作,但我想要在特定范围内以最少的拷贝和最佳效率执行此操作。我知道很多基于 stringstream 的解决方案,但我想要一些没有 stringstream 到 string 开销的东西。

最佳答案

您可以从完整容器以外的其他内容创建范围,并像在原始示例中一样使用 boost::algorithm::join

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

int main(int, char **)
{
    std::vector<std::string> list;
    list.push_back("Something");
    list.push_back("that's");
    list.push_back("not");
    list.push_back("Hello");
    list.push_back("World!");
    list.push_back("really");
    list.push_back("it's");
    list.push_back("not");

    boost::iterator_range<std::vector<std::string>::const_iterator> 
            rng (list.begin() + 3, list.begin() + 5);
    std::string joined = boost::algorithm::join(rng, ", ");
    std::cout << joined << std::endl;
}

仍然打印“Hello, World!”

关于c++ - 将范围之间的字符串 vector 转换为分隔字符串(允许 boost ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921234/

相关文章:

c++ - 使用 Boost::regex 进行正则表达式组匹配

c++ - 关于在 C++ 中使用外部库的困惑

c++程序不会在void()函数中退出循环

c++ - exit(0) 在 othewise "empty" block 中会不好吗?

c++ - 是否可以在 Xcode 调试器中输入变量来检查它们是什么?

c++ - VS Code "step into"调试器配置

c++ - 错误 C2440 : '=' : cannot convert from 'int' to 'char [5]'

c++ - boost::dynamic_properties 和不可变图形对象

Windows 的 C++ 互斥量

c++ - 对 "boost::system::system_category()"的 undefined reference