如何将std :: Regex_token_iterator复制到std :: vector
How to copy std::regex_token_iterator to a std::vector?
以下代码无法将值从regex_token_iterator复制到std :: vector;Visual Studio 2015报告说,参数的" std ::复制"可能不安全。
有人知道如何修复它吗?
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
int main()
{
// String to split in words
std::string line = "dir1\dir2\dir3\dir4";
// Split the string in words
std::vector<std::string> to_vector;
const std::regex ws_re("\\");
std::copy(std::sregex_token_iterator(line.begin(), line.end(), ws_re, -1),
std::sregex_token_iterator(),
std::back_insert_iterator<std::vector<std::string>>(to_vector));
// Display the words
std::cout << "Words: ";
std::copy(begin(to_vector), end(to_vector), std::ostream_iterator<std::string>(std::cout, "n"));
}
在这里我的解决方案将REGEX_TOKEN_ITERATOR提取的值存储到向量:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
int main()
{
std::string s("dir1\dir2\dir3\dir4");
// Split the line in words
const std::regex reg_exp("\\");
const std::regex_token_iterator<std::string::iterator> end_tokens;
std::regex_token_iterator<std::string::iterator> it(s.begin(), s.end(), reg_exp, -1);
std::vector<std::string> to_vector;
while (it != end_tokens)
{
to_vector.emplace_back(*it++);
}
// Display the content of the vector
std::copy(begin(to_vector),
end(to_vector),
std::ostream_iterator<std::string>(std::cout, "n"));
return 0;
}
非常古老的代码,但是当我也在寻找解决方案时。这是非常简单的第三个参数应为back_inserter到向量。
std::copy(std::sregex_token_iterator(line.begin(), line.end(), ws_re, -1),
std::sregex_token_iterator(),
std::back_inserter(to_vector));
相关文章:
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- std::vector的包装器,使数组的结构看起来像结构的数组
- 编译器如何区分std::vector的构造函数
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 为什么std::vector比数组慢
- std::vector::迭代器是否可以合法地作为指针
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 为什么std::vector和std::valarray初始化构造函数不同
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 在没有未定义行为的情况下实现类似std::vector的容器
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 如何将AERT_Allocate与 std:vector 一起使用
- 推导 std::vector::back() 的返回类型
- 如何将原始字节附加到 std::vector?
- std::vector 没有重载函数的实例与参数列表匹配
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?