一、str.find()

在字符串str中查询子串的位置

c++
复制代码
#include <iostream> using namespace std; int main() { string str = "abcdefg"; size_t pos = str.find("bcd"); if(pos != string::npos) { // string::npos cout << pos << endl; // pos 为 1 } }

二、str.find_first_of()

在字符串str中查询字符的位置

c++
复制代码
#include <iostream> using namespace std; int main() { string str = "abcdefg"; size_t pos = str.find_first_of("bcd"); // 查询 'b' || 'c' || 'd' 在 str 的第一个位置 if(pos != string::npos) { // string::npos cout << pos << endl; // pos 为 1 } }

三、find()

在指定范围内,查询元素的位置(一般为有迭代器的对象或容器)

c++
复制代码
#include <iostream> #include <algorithm> using namespace std; int main() { string str = "abcdef"; string::iterator pos = find(str.begin(), str.end(), 'b'); if (pos != str.end()) { cout << *pos << endl; } }

四、find_if()

在指定范围内,查询符合条件的元素的位置(一般为有迭代器的对象或容器)

c++
复制代码
#include <iostream> #include <algorithm> using namespace std; template <typename T> class condition { public: condition(const T& str): m_str(str) {}; bool operator()(const T& str) { if(str > m_str) return true; return false; } private: T m_str; }; int main() { string str = "abcdefg"; string::iterator pos = find_if(str.begin(), str.end(), condition('b')); if (pos != str.end()) { cout << *pos << endl; } }
分类: 标签:

评论

-- 评论已关闭 --

全部评论