zoukankan      html  css  js  c++  java
  • stl 的utility GIS

    std::pair

    #include <iostream>
    #include <utility>
    #include <string>
    using namespace std;
    
    int main () {
      pair <string,double> product1 ("tomatoes",3.25);
      pair <string,double> product2;
      pair <string,double> product3;
    
      product2.first = "lightbulbs";     // type of first is string
      product2.second = 0.99;            // type of second is double
    
      product3 = make_pair ("shoes",20.0);
    
      cout << "The price of " << product1.first << " is $" << product1.second << "\n";
      cout << "The price of " << product2.first << " is $" << product2.second << "\n";
      cout << "The price of " << product3.first << " is $" << product3.second << "\n";
      return 0;
    }
    utility重载了operators ==<!=>>= and <=, 这样pair 对象也可以比较了,不过先比较第一个元素 ,只有第一个元素相等的情况下才比较第2个
    utility 里面的make_pair
    pair <int,int> one;
      pair <int,int> two;
      one = make_pair (10,20);
      two = make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>
      cout << "one: " << one.first << ", " << one.second << "\n";
      cout << "two: " << two.first << ", " << two.second << "\n";
      return 0;
    c++ makepair 的的定义
    template <class T1,class T2>
      pair<T1,T2> make_pair (T1 x, T2 y)
      {
        return ( pair<T1,T2>(x,y) );
      }

    std::rel_ops

    namespace rel_ops {
      template <class T> bool operator!= (const T& x, const T& y) { return !(x==y); }
      template <class T> bool operator>  (const T& x, const T& y) { return y<x; }
      template <class T> bool operator<= (const T& x, const T& y) { return !(y<x); }
      template <class T> bool operator>= (const T& x, const T& y) { return !(x<y); }
    }
    如果使用了utility ,一个类只定义 < 和==, 其他操作符 >,<=,>=就会根据< ,自动生成代码, != 会根据== 生成代码,
    例子如下
    // rel_ops example:
    #include <iostream>
    #include <utility>
    #include <cmath>
    using namespace std;
    
    class vector2d {
    public:
      double x,y;
      vector2d (double px,double py): x(px), y(py) {}
      double length () const {return sqrt(x*x+y*y);}
      bool operator==(const vector2d& rhs) const {return length()==rhs.length();}
      bool operator< (const vector2d& rhs) const {return length()< rhs.length();} 
    };
    
    using namespace rel_ops;
    
    int main () {
      vector2d a (10,10);	// length=14.14
      vector2d b (15,5);	// length=15.81
      cout << boolalpha;
      cout << "(a<b) is " << (a<b) << "\n";
      cout << "(a>b) is " << (a>b) << "\n"; // 没有定义> ,操作符,但是有#include <UITILITY> 就行了#include <utility>

    return 0;
    }
    Output:
    
    (a<b) is true
    (a>b) is false
    
     
     
     
  • 相关阅读:
    ueditor富文本编辑在 asp.net MVC下使用步骤
    C#中Socket用法,多个聊天和单一聊天。
    事件与委托的联系和区别
    异步调用backgroudworker
    C#事件作用和用法
    如何遍历protected object转化为数组
    获取后台用户 token 的方法
    Magento2 中如何使用curl
    curl 错误排查方法
    Magento 2 REST API入门
  • 原文地址:https://www.cnblogs.com/gisbeginner/p/2839749.html
Copyright © 2011-2022 走看看