c++ - 如何在 C++ 中过滤字符串 vector ?

标签 c++ string algorithm vector filter

我有一个包含 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/

相关文章:

c++ - 使用 gdb 在 MS VC++ 2010 Express 中编译的调试程序

c++ - 在类外将部分指定的模板化矩阵乘法运算符重载函数声明为友元

C++:不能使用 std::wstringstream

c# - 如何仅从字符串中删除某些子字符串?

iphone - 如果标签相等,则更改 View

algorithm - 优先级队列数据结构的术语?

c++ - 如何使用宏#define 更改类名?

Switch 语句中的字符串 : 'String' does not conform to protocol 'IntervalType'

c++ - 在 C++ STL 中堆化?

python - 存储子图的非浪费方式