首页 > 解决方案 > C ++在包含某些字母的字符串向量中查找名称

问题描述

有谁知道在包含某些字母的 C++ 字符串向量中查找名称的好方法?

假设我有一个包含 100 个名称的向量。有没有办法找到例如所有包含“t”字符的名字,比如汤姆、彼得、乔纳森等等?

据我了解,如果我专门搜索“Jonathan”,std::find 只会找到名称 Jonathan。还有另一种可以使用的方法吗?

标签: c++stringvector

解决方案


https://en.cppreference.com/w/cpp/algorithm/partition

auto it = std::partition(v.begin(), v.end(), [](std::string s) {
return ( s.find("t") != std::string::npos );
});

推荐阅读