结构体

建立一个结构体,内含构造函数

struct point {
    int x,y;
    // =0 表示默认值为0 如果没有指定参数的值 就按0处理  
    point(int x=0,int y=0):x(x),y(y) {}
    // x(x):x的值初始化为x
};

另一种写法

point(int x=0,int y=0) { 
    this->x=x; 
    this->y=y; 
} 

计算结构体的和

point operator + (const point& A,const point &B) {
    return point(A.x+B.x,A.y+B.y);
}

输出流

ostream& operator << (ostream &out,const point& p) {
    out<<p.x<<" "<<p.y;
    return out;
}

定义结构体时 如:point a,b(1,2); 

此时分别调用了point(),和point(1,2)

#include <iostream>
using namespace std;

struct point {
    int x,y;
    point(int x=0,int y=0):x(x),y(y) {}
};
point operator + (const point& A,const point &B) {
    return point(A.x+B.x,A.y+B.y);
}
ostream& operator << (ostream &out,const point& p) {
    out<<p.x<<" "<<p.y;
    return out;
}
int main( ) {
    point a,b(1,2);
    a.x=3;
    cout<<a+b<<endl;
    return 0;
}
View Code

 求和函数,可求double和结构体

T sum(T* begin,T* end) {
    T *p=begin;
    T ans=0;
    for(T *p=begin;p!=end;p++) ans=ans+*p;
    return ans;
}
double a[]={1.1,2.2,3.3,4.4};
cout<<sum(a,a+4)<<endl;
template<typename T> //定义
struct point {
    T x,y;
    point(T x=0,T y=0) : x(x),y(y) {}
};

  

#include <iostream>
using namespace std;
template<typename T>
struct point {
    T x,y;
    point(T x=0,T y=0) : x(x),y(y) {}
};
template<typename T>
point<T> operator + (const point<T>& A,const point<T>& B) {
    return point<T>(A.x+B.x,A.y+B.y);
}
template<typename T>
ostream& operator << (ostream &out,const point<T>& p) {
    out<<p.x<<" "<<p.y;
    return out;
}
int main( ) {
    point<int>a(1,2),b(3,4);
    point<double>c(1.1,2.2),d(3.3,4.4);
    cout<<a+b<<" "<<c+d<<endl;
    return 0;
} 
View Code

优先队列结构体

struct node {
    int data;
    int t;
    bool operator()(node a,node b) {
        return a.t>=b.t;
    }
}
priority_queue< node,vector<node>,node> pq;
node t;
pq.push(t);
View Code

 map类结构体

struct nod {
    int x;
    int y;
    bool operator < (const nod &b) const {
        if(x==b.x) return y<b.y;
        return x<b.x; 
    }
    bool operator == (const nod &b) const {
        return x==b.x&&y==b.y;
    } 
}p[maxn];

map<nod,int>m;

m[ {p[i].x,p[j].y} ]++;

map<nod,int>::iterator iter;
for(iter=m.begin();iter!=m.end();iter++) 
k=min(k,n-(iter->second));
原文地址:https://www.cnblogs.com/iwomeng/p/11260890.html