C++中pair的用法以及函数的多个返回值的用法

注意:文章为转载多人的博客,我只是综合一下。

pair的类型

    pair 是 一种模版类型。每个pair 可以存储两个值。这两种值无限制。也可以将自己写的struct的对象放进去。。

    pair<string,int> p;

    pair<int ,int > p;

   pair<double,int> p;

  都可以。。。  

   应用:如果一个函数有两个返回值 的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct ,但为了方便就可以使用 c++  自带的pair ,返回一个pair,其中带有两个值。除了返回值的应用,在一个对象有多个属性的时候 ,一般自己写一个struct ,如果就是两个属性的话,就可以用pair 进行操作。。。

    应用pair 可以省的自己写一个struct 。。。如果有三个属性的话,其实也是可以用的pair 的 ,极端的写法 pair <int ,pair<int ,int > >

写法极端。(后边的两个 > > 要有空格,否则就会是 >>  位移运算符)

makr_pair:

   pair<int ,int >p (5,6);

   pair<int ,int > p1= make_pair(5,6);

   pair<string,double> p2 ("aa",5.0);

   pair <string ,double> p3 = make_pair("aa",5.0);

有这两种写法来生成一个pair


 如何取得pair的值呢。。

 每个pair 都有两个属性值  first  和second

 cout<<p1.first<<p1.second; 

 注意是属性值而不是方法。

由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:

typedef pair<string, string> author;

author pro("May", "Lily");

author joye("James", "Joyce");

pair的使用:

#include <cstdio>  
#include <algorithm>  
#include <iostream>  
#include <string>  
#include <map>  
#include <vector>  
#include <set>  
#include <cmath>  
#include <stack>  
#include <queue>  
#include <cstring>  
#include <utility>  
  
using namespace std;  
  
#define ll long long  
#define llu unsigned long long  
#define INF 100000000  
  
//使用pair表示状态时,使用typedef会更加方便一些  
const int maxn = 100+10;  
typedef pair<int, int> P;  
char maze[maxn][maxn];//表示迷宫的字符串数组  
int n,m;  
int sx,sy;//起点坐标  
int gx,gy;//重点坐标  
int d[maxn][maxn]; //到各个位置的最短距离的数组  
//四个方向移动的向量  
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};  
  
//求从(sx,sy)到(gx,gy)的最短距离  
//如果无法到达,则是INF  
int bfs()  
{  
    queue<P > que;  
    // 把所有的位置都初始化为INF  
    for(int i = 0; i < n; ++i)  
        for(int j = 0; j < m; ++j) d[i][j] = INF;  
    //将起点加入队列,并把这一地点的距离设置为0  
    que.push(P(sx,sy));  
    d[sx][sy] = 0;  
    //不断循环直到队列的长度为0  
    while(que.size()) //while(!que.empty())  
    {  
        //从队列的最前端取出元素  
        P p = que.front();  
//        cout << p.first << " " << p.second << endl;  
        que.pop();  
        //如果取出的状态已经是终点,则结束搜索  
        if(p.first == gx && p.second == gy) break;  
        //四个方向的循环  
        for(int i = 0; i < 4; ++i)  
        {  
            //移动之后的位置记为(nx,ny)  
            int nx = p.first + dx[i],ny = p.second + dy[i];  
            //判断是否可以移动以及是否已经访问过(d[nx][ny] != INF即为已经访问过)  
            if(0 <= nx && nx < n && 0 <= ny && ny < m && maze[nx][ny] != '#'  
                    && d[nx][ny] == INF)  
            {  
                //可以移动的话则加入队列,并且到该位置的距离确定为到p的距离+1  
                que.push(P(nx,ny));  
                d[nx][ny] = d[p.first][p.second] + 1;  
            }  
        }  
    }  
    return d[gx][gy];  
}  
void solve()  
{  
    int res = bfs();  
    printf("%d\n",res);  
}  
int main()  
{  
#ifndef ONLINE_JUDGE  
    freopen("in.txt","r",stdin);  
//    freopen("out.txt","w",stdout);  
#endif // ONLINE_JUDGE  
    while(cin >> n >> m)  
    {  
        memset(maze,0,sizeof(maze));  
        for(int i = 0; i < n; ++i)  
            for(int j = 0; j < m; ++j)  
            {  
                cin >> maze[i][j];  
                if(maze[i][j] == 'S') {sx = i; sy = j;}  
                if(maze[i][j] == 'G') {gx = i; gy = j;}  
            }  
//        cout << sx << " " << sy << endl;  
//        for(int i = 0; i < n; ++i)  
//        {  
//            for(int j = 0; j < n; ++j)  
//                cout << maze[i][j];  
//            cout << endl;  
//        }  
        solve();  
    }  
    return 0;  
}  

struct的使用:

//有n个结构体变量,内含学生号, 姓名和三门课成绩。要求输出平均成绩最高学生的信息。  
#include <stdio.h>  
#define N 3  
struct Student  
{  
    int num;  
    char name[20];  
    float socre[3];  
    float aver;  
};  
void input(struct Student stu[])  
{  
    int i;  
    printf("请输入各学生的信息:学号,姓名,三门课成绩:\n");  
    for(i=0;i<N;i++)  
    {  
        scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].socre[0],&stu[i].socre[1],&stu[i].socre[2]);  
        stu[i].aver=(stu[i].socre[0]+stu[i].socre[1]+stu[i].socre[2])/3;  
    }  
}  
struct Student max(struct Student stu[])  
{  
    int i,m=0;  
    for(i=0;i<N;i++)  
        if(stu[i].aver>stu[m].aver)  
            m=i;  
    return stu[m];  
}  
void print(struct Student stud)  
{  
    printf("\n成绩最高的学生是\n");  
    printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.socre[0],stud.socre[1],stud.socre[2],stud.aver);  
}  
int main()  
{  
    struct Student stu[N],*p=stu;  
    input(p);  
    print(max(p));  
    return 0;  
} 


猜你喜欢

转载自blog.csdn.net/qq_40727946/article/details/79907769