From 4cc74bc0bb197dccaafdcd26be78224ba1899422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 10 Oct 2014 19:29:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90find=5Ffirst=5Fof?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/String.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/TinySTL/String.h b/TinySTL/String.h index a3dd121..a175a84 100644 --- a/TinySTL/String.h +++ b/TinySTL/String.h @@ -210,6 +210,13 @@ namespace TinySTL{ size_t rfind_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const; size_t find_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const; int compare_aux(size_t pos, size_t len, const_iterator cit, size_t subpos, size_t sublen)const; + bool isContained(char ch, const_iterator first, const_iterator last)const{ + for (auto cit = first; cit != last; ++cit){ + if (*cit == ch) + return true; + } + return false; + } public: friend std::ostream& operator <<(std::ostream& os, const string&str); friend std::istream& operator>> (std::istream& is, string& str); @@ -594,6 +601,22 @@ namespace TinySTL{ int string::compare(size_t pos, size_t len, const char* s, size_t n) const{ return compare_aux(pos, len, s, 0, n); } + size_t string::find_first_of(const string& str, size_t pos) const{ + return find_first_of(str.begin(), pos, str.size() - pos); + } + size_t string::find_first_of(const char* s, size_t pos) const{ + return find_first_of(s, pos, size() - pos); + } + size_t string::find_first_of(const char* s, size_t pos, size_t n) const{ + for (size_t i = pos; i != pos + n; ++i){ + if (isContained((*this)[i], s, s + strlen(s))) + return i; + } + return npos; + } + size_t string::find_first_of(char c, size_t pos) const{ + return find(c, pos); + } std::ostream& operator <<(std::ostream& os, const string&str){ for (const auto ch : str){ os << ch;