eis/mix_cc/algorithm/is_found.h

58 lines
1.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file mix_cc/algorithm/is_found.h
* @brief 在容器中是否可以找到对应的元素/迭代器
* @author Cat (null.null.null@qq.com)
* @version 0.1
* @date 2021-05-07
*
* Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION
*
*/
#pragma once
#include <utility>
#include <typeindex>
#include <algorithm>
namespace mix_cc {
/**
* @brief 普通的元素查询如果元素在容器中返回true否则返回false
* @tparam Container
* @tparam T
* @param container 容器
* @param element 元素
* @return std::enable_if_t<!std::is_same<typename Container::iterator, T>::value, bool>
*/
template <typename Container, typename T>
std::enable_if_t<!std::is_same<typename Container::iterator, T>::value, bool>
is_found(const Container &container, const T &element) {
bool ret = true;
// equals end, means there is NO element in container
if (container.find(element) == container.end()) {
ret = false; // we can't find
}
return ret;
}
/**
* @brief 迭代器查询如果迭代器在容器中返回true否则返回false
* @tparam Container
* @tparam I
* @param container 容器
* @param iter 迭代器
* @return std::enable_if_t<std::is_same<typename Container::iterator, I>::value, bool>
*/
template <typename Container, typename I>
std::enable_if_t<std::is_same<typename Container::iterator, I>::value, bool>
is_found(const Container &container, const I &iter) {
bool ret = true;
// equals end, means there is NO element in container
if (iter == container.end()) {
ret = false; // we can't find
}
return ret;
}
} // namespace mix_cc