cpp依照正则表达式分割字符串函数返回值类型是stdvectorstdstring以1个stdstring为分割
以下是一个使用正则表达式分割字符串的示例函数,返回值类型为std::vectorstd::string。
#include <iostream>
#include <vector>
#include <string>
#include <regex>
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter) {
std::vector<std::string> result;
std::regex regex(delimiter);
std::sregex_token_iterator iterator(input.begin(), input.end(), regex, -1);
std::sregex_token_iterator end;
while (iterator != end) {
result.push_back(*iterator);
++iterator;
}
return result;
}
int main() {
std::string input = "Hello,World,This,is,a,test";
std::string delimiter = ",";
std::vector<std::string> result = splitString(input, delimiter);
for (const auto& str : result) {
std::cout << str << std::endl;
}
return 0;
}
此函数将输入字符串input按照正则表达式delimiter进行分割,并将分割后的子串存储在std::vectorstd::string中返回。在示例中,输入字符串为"Hello,World,This,is,a,test",分隔符为",",输出结果为:
Hello
World
This
is
a
test
``
原文地址: https://www.cveoy.top/t/topic/h2Eh 著作权归作者所有。请勿转载和采集!