c++20 ostream实现字符串分割
在C++20中,可以使用ranges库中的split_view来实现字符串分割。split_view是一个视图,它可以将字符串分割成一系列子字符串。
以下是一个使用split_view实现字符串分割的例子:
#include <iostream>
#include <string>
#include <ranges>
int main() {
std::string str = "hello,world,how,are,you";
auto split = str | std::views::split(',');
for (const auto& sub : split) {
std::cout << sub << '\n';
}
return 0;
}
在这个例子中,我们使用split函数将字符串按照逗号分割成一系列子字符串,并遍历输出每个子字符串。
输出结果:
hello
world
how
are
you
注意,使用split_view需要包含头文件
原文地址: https://www.cveoy.top/t/topic/sFo 著作权归作者所有。请勿转载和采集!