c++20 实现字符串分割
在C++20中,可以使用std::ranges::split_view函数来实现字符串分割。
例如,假设有一个字符串str,需要将其按照空格分割成多个子字符串,可以使用以下代码:
#include <iostream>
#include <string>
#include <ranges>
int main() {
std::string str = "hello world";
for (auto&& word : str | std::views::split(' ')) {
std::cout << word << std::endl;
}
return 0;
}
运行结果为:
hello
world
在std::ranges::split_view函数中,第一个参数为需要分割的字符串,第二个参数为分隔符。在以上代码中,使用空格作为分隔符。
使用std::ranges::split_view函数可以方便地实现字符串分割,同时也可以使用其他函数进行更复杂的分割操作。
原文地址: https://www.cveoy.top/t/topic/sED 著作权归作者所有。请勿转载和采集!