随笔之~ << find string.find

//1.string<<
stringstream ss;
ss.clear();
ss.str("");
ss << st.year <<st.month<<st.day; //int 2015 int 1 int 8
string strDate;
ss >> strDate; //strDate:201518
int iTimeId = st.hour*12 + st.minute/5ios

 

 

//2.位数
CString szTimeId;
szTimeId.Format("%d", 1000 + iTimeId);
string strTimeId(szTimeId.Right(3).GetBuffer()); //保证取到的是3位数c++

 

 

 

//3. find
STL的find,find_if函数提供了一种对数组、STL容器进行查找的方法。使用该函数,需 #include <algorithm>
咱们查找一个list中的数据,一般用find(),例如:数组

using namespace std;
int main()
{
    list<int> lst;
    lst.push_back(10);
    lst.push_back(20);
    lst.push_back(30);
    list<int>::iterator it = find(lst.begin(), lst.end(), 10); // 查找list中是否有元素“10”
    if (it != lst.end()) // 找到了
    {
        // do something
    }
    else // 没找到
    {
        // do something
    }
    return 0;
}函数

那么,若是容器里的元素是一个类呢?例如,有list<CPerson> ,其中CPerson类定义以下:spa

class CPerson
{
public:
    CPerson(void);
    ~CPerson(void);指针

public:
    int age; // 年龄
};orm

那么如何用find()函数进行查找呢?这时,咱们须要提供一个判断两个CPerson对象“相等”的定义,find()函数才能从一个list中找到与指定的CPerson“相等”的元素。
这个“相等”的定义,是经过重载“==”操做符实现的,咱们在CPerson类中添加一个方法,定义为:
bool operator==(const CPerson &rhs) const;
实现为:
bool CPerson::operator==(const CPerson &rhs) const
{
    return (id == rhs.age);
}对象

而后咱们就能够这样查找(假设list中已经有了若干CPerson对象)了:
list<CPerson> lst;
//////////////////////////////////
// 向lst中添加元素,此处省略
//////////////////////////////////
CPerson cp_to_find; // 要查找的对象
cp_to_find.age = 50;
list<CPerson>::iterator it = find(list.begin(), list.end(), cp_to_find); // 查找索引

if (it != lst.end()) // 找到了
{
    // do something
}
else // 没找到
{
    // do something
}
这样就实现了需求。ci

有人说,若是我有本身定义的“相等”呢?例如,有一个list<CPerson*>,这个list中的每个元素都是一个对象的指针,咱们要在这个list中查找具备指定age的元素,找到的话就获得对象的指针。
这时候,你再也不能像上面的例子那样作,咱们须要用到find_if函数,并本身指定predicate function(即find_if函数的第三个参数,请查阅STL手册)。先看看find_if函数的定义:
template<class InputIterator, class Predicate>
InputIterator find_if(InputIterator _First, InputIterator _Last, Predicate _Pred);
Parameters
_First
An input iterator addressing the position of the first element in the range to be searched.
_Last
    An input iterator addressing the position one past the final element in the range to be searched.
_Pred
    User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.


咱们在CPerson类外部定义这样一个结构体:
typedef struct finder_t
{
    finder_t(int n) : age(n) { } bool operator()(CPerson *p) { return (age == p->age); } int age;
}finder_t;

而后就能够利用find_if函数来查找了:
list<CPerson*> lst;
//////////////////////////////////
// 向lst中添加元素,此处省略
//////////////////////////////////

list<CPerson*>::iterator it = find_if(lst.begin(), lst.end(), finder_t(50)); // 查找年龄为50的人
if (it != lst.end()) // 找到了
{
    cout << "Found person with age : " << (*it)->age;
}
else // 没找到
{
    // do something
}

 

 

 

// 4.string.find()

#include <string>
#include <iostream>
using namespace std;
void main()
{
    [cpp] view plaincopyprint?
     ////find函数返回类型 size_type  
    string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i"); 
    string flag; 
    string::size_type position; 
     
    //find 函数 返回jk 在s 中的下标位置   
    position = s.find("jk"); 
    if (position != s.npos)  //若是没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,  
    { 
     cout << "position is : " << position << endl; 
    } 
    else 
    { 
     cout << "Not found the flag" + flag; 
    } 
 
[cpp]
1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置  
2.  flag = "c"; 
3.  position = s.find_first_of(flag); 
4.  cout << "s.find_first_of(flag) is : " << position << endl; 
[cpp]
1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标  
2. position=s.find("b",5); 
3. cout<<"s.find(b,5) is : "<<position<<endl; 
[cpp]
1. //查找s 中flag 出现的全部位置。  
2.  flag="a"; 
3.  position=0; 
4.  int i=1; 
5.  while((position=s.find_first_of(flag,position))!=string::npos) 
6.  { 
7.   //position=s.find_first_of(flag,position);  
8.   cout<<"position  "<<i<<" : "<<position<<endl; 
9.   position++; 
10.   i++; 
11.  }     
[cpp]
1. //查找flag 中与s 第一个不匹配的位置  
2. flag="acb12389efgxyz789"; 
3. position=flag.find_first_not_of (s); 
4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;    
[cpp]
1.  //反向查找,flag 在s 中最后出现的位置  
2.  flag="3"; 
3.  position=s.rfind (flag); 
4.  cout<<"s.rfind (flag) :"<<position<<endl;
5. } 

 说明:1.  若是string sub = ”abc“;              string s = ”cdeabcigld“;     s.find(sub) , s.rfind(sub) 这两个函数,若是彻底匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。     s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。2.  若是没有查询到,则返回string::npos,这是一个很大的数,其值不须要知道

相关文章
相关标签/搜索