find

1. std::find()

template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
    while (first!=last) {
        if (*first==val) return first;
        ++first;
    }
    return last;
}

注意点:

#include <algorithm> // std::find

假如找不到,返回last,而不是固定的end()!

2. string::find()

if (str.find("xxx") != string::npos)

从指定位置(index)开始find:str.find("xxx", 3)

3. STL find

包括map,set,stack,queue等

if (map.find(key) != map.end()) // 假如找到了key

注意:vector竟然没有find() !!!所以需要用泛型的find()。

原文地址:https://www.cnblogs.com/Younger-Zhang/p/15168419.html