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
    
     
     
     
  • 相关阅读:
    sqlserver的排序
    tomcat虚拟目录!
    tomcat虚拟主机配置!
    深度优先搜索算法
    java是什么?
    tomcat服务器jar管理!
    广度优先搜索算法
    简单web服务器
    cascade="alldeleteorphan"时容易出现的一个异常
    Hibernate级联删除的一个错误
  • 原文地址:https://www.cnblogs.com/gisbeginner/p/2839749.html
Copyright © 2011-2022 走看看