假设我有以下对象:
vector<string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};
我试图在数据对象中找到第一个非重复条目。例如,data.find_first_not_of(data.at(0)); 如果数据仅为字符串类型(无容器),这将起作用。
我怎样才能用向量类型的对象实现同样的事情。
我从算法库中查看了 neighbor_find 和 find_if_not,但无济于事。
非常感谢您的建议。
你遇到了什么问题adjacent_find
?您应该能够将其与逆谓词一起使用:
std::vector<std::string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};
// Sort data here if necessary
auto itr = std::adjacent_find(data.cbegin(), data.cend(), std::not_equal_to<std::string>{});
if (itr != data.cend()) {
std::cout << "First mismatch: " << *itr << " " << *std::next(itr) << std::endl;
} else {
std::cout << "All elements equal" << std::endl;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句