例子:
template<class fun>
void mytest(fun f)
{_string s1 = "abc";_string s2 = "abc";if (f(s1, s2)){std::cout << "相等。\n";}}int main()
{mytest([](const _string s1, const _string& s2) { return s1 == s2; });return 0;}
输出:
下面是模拟C++标准库的应用:
#include "X:\Work\Share\CCode\CPlatform\MathExt\_MathExt_out.h"using namespace lf;
using namespace std;int main()
{std::string s1 = "abcd1111";s1.erase(std::find_if(s1.rbegin(), s1.rend(), [](char ch) { return ch != '1'; }).base(), s1.end());_string s2 = "abcd1111";s2.std_erase(lf::_find_if(s2.std_rbegin(), s2.std_rend(), [](char ch) { return ch != '1'; }).std_base(), s2.end());std::cout << "s1=" << s1 << "\n";_pcn(s2);}
输出:
代码:
/*******************************************************************************************
文件名 : _xutility.h作者 : 李锋功能 : 模拟 <xutility>创建时间 : 2024年04月21日最后一次修改时间 : 2024年04月21日
********************************************************************************************/
#pragma once;
#include "_Macro.h"_LF_BEGIN_/*
_EXPORT_STD template <class _Category, class _Ty, class _Diff = ptrdiff_t, class _Pointer = _Ty*,class _Reference = _Ty&>
struct _CXX17_DEPRECATE_ITERATOR_BASE_CLASS iterator { // base type for iterator classesusing iterator_category = _Category; //表示迭代器的类别,例如输入、输出、前向、双向或随机访问等。using value_type = _Ty; //表示迭代器所指向元素的类型。using difference_type = _Diff; //表示迭代器之间的差值类型,默认为ptrdiff_t。using pointer = _Pointer; //表示迭代器所指向元素的指针类型。using reference = _Reference; //表示迭代器所指向元素的引用类型。
};_CXX17_DEPRECATE_ITERATOR_BASE_CLASS是一个宏定义,用于在C++17中标记迭代器基类
(如std::iterator)的弃用。这个宏定义通常用于告知开发者,该迭代器基类在未来的版
本中可能会被移除或更改,建议使用其他替代方案。_EXPORT_STD是一个宏定义,通常用于在标准库的头文件中。它的作用是在编译时将一些符号标记为导出(export),
以便在其他模块中使用。这样做的目的是避免在链接时出现重复定义的错误。然而,这个宏在C++17中已经被移除,
因为它与模块化的支持有关,而模块化在C++17中并未完全实现。
*//// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Isbackward"></typeparam>
/// <typeparam name="Step"></typeparam>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-21
template <typename T, size_t Step = 1, bool Isbackward = true>
class _iterator_base {
public:T* _pt;
public:inline _iterator_base(const T* pt) : _pt((T*)pt) {}
public:/// <summary>/// 类型转换/// </summary>inline operator const T* () const { return _pt; }/// <summary>/// 前置加加/// </summary>/// <returns></returns>_iterator_base& operator++() {//效率高if (Isbackward)_pt = _pt + Step;else_pt = _pt - Step;return *this;}/// <summary>/// 后置加加/// </summary>/// <param name=""></param>/// <returns></returns>_iterator_base operator++(int) { _iterator_base oldvalue = *this;if (Isbackward)_pt = _pt + Step;else_pt = _pt - Step;return oldvalue;}/// <summary>/// 前置减减/// </summary>/// <returns></returns>inline _iterator_base& operator--() {if (Isbackward)_pt = _pt - Step;else_pt = _pt + Step;return *this;}/// <summary>/// 后置减减/// </summary>/// <param name=""></param>/// <returns></returns>_iterator_base operator--(int){_iterator_base oldvalue = *this;if (Isbackward)_pt = _pt - Step;else_pt = _pt + Step;}inline bool operator==(const _iterator_base& right) {return _pt == right._pt;}inline bool operator<(const _iterator_base& right) {if (Isbackward)return _pt < right._pt;elsereturn _pt > right._pt; }inline bool operator<=(const _iterator_base& right) {if (Isbackward)return _pt <= right._pt;elsereturn _pt >= right._pt;}inline bool operator>(const _iterator_base& right) {if (Isbackward)return _pt > right._pt;elsereturn _pt < right._pt;}// 解引用操作符重载inline const T& operator*() const{ return *_pt; }/*// 获取原始指针的操作符重载T* operator->() const {return _pt;}*/inline const T* std_base() { return _pt; }
};template <typename T>
class _iterator : public _iterator_base<T,1,true>
{
public:inline _iterator(const T* pt) : _iterator_base<T, 1, true>(pt) {}
};template <typename T>
class _reverse_iterator : public _iterator_base<T, 1, false>
{
public:inline _reverse_iterator(const T* pt) : _iterator_base<T, 1, false>(pt) { }
};/// <summary>
/// 模拟std::find_if
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="fun"></typeparam>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="pred"></param>
/// <returns></returns>
/// 创建时间:2024-04-21 最后一次修改时间:2024-04-22
template <class T, class fun>
_reverse_iterator<T> _find_if(const _reverse_iterator<T>& first,const _reverse_iterator<T>& last, fun pred) {/* 用法std::string s1 = "0abc023030000";s1.erase(std::find_if(s1.rbegin(), s1.rend(),[](char ch) { return ch != '0'; }).base(), s1.end());std::cout << "s1=" << s1 << "\n";_string s2 = _t("0abc023030000");s2.std_erase(lf::_find_if(s2.std_rbegin(), s2.std_rend(),[](wchar_t ch) { return ch != _t('0'); }).std_base(), s2.std_end());std::wcout << _t("s2=") << s2 << _t("\n");*/_reverse_iterator<T> itStart = first;++itStart;for (; itStart <= last; itStart++) {if (pred(*itStart)) {return itStart + 1; //符合条件字符串的下一位置。}}return last;
}_LF_END_
_string中的代码:
/// <summary>/// 模拟std::string::erase/// </summary>/// <param name="first"></param>/// <param name="last"></param>/// <returns></returns>/// 创建时间: 2024-04-21 最后一次修改时间:2024-04-22_Str<T>& std_erase(const _iterator<T>& first, const _iterator<T>& last) {/*std::string s1 = "0abc023030000";s1.erase(std::find_if(s1.rbegin(), s1.rend(),[](char ch) { return ch != '0'; }).base(), s1.end());std::cout << "s1=" << s1 << "\n";_string s2 = _t("0abc023030000");s2.std_erase(lf::_find_if(s2.std_rbegin(), s2.std_rend(),[](wchar_t ch) { return ch != _t('0'); }).std_base(), s2.std_end());std::wcout << _t("s2=") << s2 << _t("\n");*/_Str<T> tmp(_t(""), _nLength);const T* pf = first;T* pl = (T*)(const T*)last;int n = pf - _pData;//拷贝前半部分if (n > 0){for (int i = 0; i < n; ++i){tmp.Add(*(_pData + i));}}++pl;//拷贝后半部分while (*pl) {tmp.Add(*pl);++pl;}//清除字符,注意,在这里不要清除内存this->Clear();this->Add(tmp); //拷贝进来return *this;}