是否有用于执行反向字符串拆分器的开箱即用功能?
Is there an out of the box function for performing the reverse a string splitter?
为了将字符串拆分为我使用的向量
std::vector<std::string> v;
boost::split(v, input, boost::is_any_of("|"));
Boost 或 STL 中是否有一个函数可以执行此操作的相反操作,即形式的连接函数
join(v, output, "|")
有boost::join
:
std::vector<std::string> v = {"Hello", "world"};
const std::string separator = " ";
std::string s = boost::join(v, separator);
演示
是的,有这样一个函数,它被称为std::accumulate
:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> v { "a", "b" };
std::string r = std::accumulate( v.begin(), v.end(), std::string(""),[](std::string a,std::string b){ return a + " | " + b; });
std::cout << r;
}
输出:
| a | b
正确处理边界(开始时没有|
(,代码变得更加冗长:相应地传递v.begin() + 1
和初始字符串,但要注意空v
的边缘情况。
然而,并不是说这种天真的std::accumulate
应用远非高效(有关详细信息,请参阅此处(。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- C++将向量的向量拆分为向量的N个子向量
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 使用Qt将应用程序的功能拆分为插件
- C++中的拆分功能
- 是否有用于执行反向字符串拆分器的开箱即用功能?
- const char和拆分功能
- 为什么此拆分功能不起作用
- 如何拆分功能:绑定
- 具有全局功能的hpp / cpp拆分:.的多重定义.错误