zoukankan      html  css  js  c++  java
  • C++ STL set::find的用法

     

    参考:

    http://blog.csdn.net/lihao21/article/details/6302196

    /* class for function predicate
        * - operator() returns whether a person is less than another person
        */
       class PersonSortCriterion {
         public:
           bool operator() (const Person& p1, const Person& p2) const {
               /* a person is less than another person
                * - if the last name is less
                * - if the last name is equal and the first name is less
                */
               return p1.lastname()<p2.1astname() ||
                      (! (p2.1astname()<p1.lastname()) &&
                       p1.firstname()<p2.firstname());
           }
       };

    set的find查找相等原理

    http://bbs.csdn.net/topics/390237400

    向set中添加的元素类型必须重载<操作符用来排序。排序满足以下准则:
    1、非对称,若A<B为真,则B<A为假。
    2、可传递,若A<B,B<C,则A<C。
    3、A<A永远为假。
    set中判断元素是否相等:
    if(!(A<B || B<A)),当A<B和B<A都为假时,它们相等。

    以上好处可以直接不通过指针而通过对象来查找

    class  Data
    {
    public:
        Data(int view, const char* key)
            : view_(view),
            key_(key),
            data_(NULL) {}
        ~Data() {}
    
    public:
        int view_;
        const char* key_;
        void* data_;
    };
    
    class DataComparator {
    public:
        bool operator()(const Data* d1, const Data* d2) const {
            return (d1->view_ == d2->view_) ? (d1->key_ < d2->key_) :
                (d1->view_ < d2->view_);
        }
    };
    
    
     typedef std::set<Data*, DataComparator> DataSet;
    
    void main() 
    { 
        DataSet dataSet;
        dataSet.insert(new Data(1,"b"));
        DataSet::iterator iter=dataSet.find(new Data(1,"b"));
    } 
  • 相关阅读:
    python字典实现原理-哈希函数-解决哈希冲突方法
    ElasticSearch-倒排索引
    ElasticSearch-核心概念
    MarkdownPad2基础语法
    下载python3.6,进行编译安装,运行django程序
    linux-指令1
    注解和反射
    Htlm和Css
    JAVASE加强
    网络编程
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/3252136.html
Copyright © 2011-2022 走看看