Files
dl_old/dl/base/dl_range_sub.h
T
2023-11-06 23:48:32 +08:00

72 lines
1.2 KiB
C++

/**
* @file dl_range_sub.h
* @brief 子串操作
*
*
* @version 1.0
* @author lveyou
* @date 23-06-22
*
* @note
*/
#pragma once
//! 类似子串的操作
namespace dl::Sub
{
/**
* @brief 子串匹配
* @param a 容器a
* @param b 容器b
* @retval b是a的子串,则返回true
*/
template<typename T>
bool IsSub(const T& a, const T& b)
{
return std::search(a.begin(), a.end(), b.begin(), b.end()) != a.end();
}
/**
* @brief 子串匹配
* @retval b是a的子串,并且在头部,则返回true
*/
template<typename T>
bool IsBegin(const T& a, const T& b)
{
auto iter_a = a.begin();
auto iter_b = b.begin();
while (iter_b != b.end())
{
if (iter_a == a.end())
return false;
if (*iter_b != *iter_a)
return false;
++iter_a;
++iter_b;
}
return true;
}
/**
* @brief 区间匹配
* @retval 从c中取得 (a, b)
* @note 传入name(xx)yy)、(、),返回xx
*/
template<typename T, typename V>
T Range(const T& c, const V& a, const V& b)
{
auto iter_a = std::find(c.begin(), c.end(), a);
if (iter_a == c.end())
return {};
++iter_a;
auto iter_b = iter_a;
while (iter_b != c.end())
{
if (*iter_b == b)
return { iter_a, iter_b };
++iter_b;
}
return {};
}
}