我有一个包含 10000 个字符串的大 vector :
std::vector<std::string> v;
for (int i = 0; i < 10000; i++) { v.push_back(generateRandomString(10)); }
我想显示包含“AB”作为子字符串的字符串。我试过:
std::vector<std::string> res;
res = std::copy_if(v, [](auto s) { return s.find("AB") != std::string::npos; });
cout << res;
但是我得到以下错误:
error: no matching function for call to 'copy_if(std::vectorstd::__cxx11::basic_string<char >&, main(int, char**)::<lambda(auto:1)>)' std::vectorstd::string b = std::copy_if(a, [](auto s) { return s.find("AB") != std::string::npos; });
如何过滤字符串 vector 并仅显示以“AB”为子字符串的字符串?
(如果 v
包含 50MB 的数据,这是否有效?)
最佳答案
类似的东西(未经测试):
std::copy_if(v.begin(), v.end(),
std::ostream_iterator<std::string>(std::cout, "\n"),
[](const std::string& s) { return s.find("AB") != std::string::npos; });
关于c++ - 如何在 C++ 中过滤字符串 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44791816/