【发布时间】:2021-12-03 15:05:15
【问题描述】:
如何使用 std::ranges 连接两个视图?
在 range-v3 中,视图与 views::concat() 连接,我无法弄清楚如何使用 std::ranges 来实现。
#include <string>
#include <iostream>
#include <vector>
#include <ranges>
using namespace std;
using namespace views;
void snake_to_camel() {
auto transform = views::transform;
auto const s = string{"feel_the_force"};
auto words = s | split('_'); // [[f,e,e,l],[t,h,e],[f,o,r,c,e]]
auto words_cap = words | transform([](auto word){
auto transform = views::transform;
auto head = word | take(1)
| transform([](unsigned char c){return toupper(c);}); // e.g. [F]
auto tail = word | drop(1); // e.g. [e.e.l]
return tail;
//return concat( head, tail ); // e.g. [F,e,e,l] // join( head, tail ) will not work here.
}); // [[F,e,e,l],[T,h,e],[F,o,r,c,e]]
auto s_camelcase = words_cap | join; // [F.e.e.l.T.h.e.F.o.r.c.e]
string res;
for(auto c : s_camelcase)
res.push_back(c);
cout << res;
}
int main() {
snake_to_camel();
cout << endl;
}
【问题讨论】:
-
棘手。不幸的是,像
std::array parts{head, tail}; return join(parts);这样的东西不起作用。恐怕你必须自己重新实现ranges-v3的views::concat(),或者当然只使用ranges-v3。
标签: c++ c++20 std-ranges