gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:13:15 30 4
gpt4 key购买 nike

我有一个大小为 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com