c++ pair详解

总述:

介绍pair的基本用法,包括pair的创建,排序,使用特性等

1.pair的创建

a.pair<int,int> p或者pair<int,int>p(0,1).

b.可以使用make_pair()函数创建一个临时的pair变量,常用作pair作为函数参数时,参数的赋值。make_pair(0,1)

c.一般我们可以在using namespace std;这一句的后面加上typedef pair<int,int> P,給他一个别名,那么我们就可以简单的声明P(1,1).

可以通过p.first和p.second分别访问第一个和第二个元素。

示例:

声明一个函数,函数参数为一个pair

void demo(pair<string,int> p){
    cout<<p.first<<"  "<<p.second<<endl;
}
int main(){
    pair<string,int> example1("hello",1);
    demo(example1);
    demo(make_pair("xiaoming",2));
    return 0;
}

有了别名之后:

#include<iostream>
using namespace std;
typedef pair<int,int> P

int main(){
    P p(0,1);
    P pp = make_pair(0,1);
    return 0;
}

2.pair的排序

pair默认是先对第一个关键字从小到大排序,如果第一关键字相同,在对第二关键字从小到大排序,都是升序

#include<iostream>
#include<vector>
using namespace std;
typedef pair<int,int> P

int main() {
    P p1 = P(1,3);
    P p2 = P(2,2);
    P p3 = P(3,1);
    P p4 = P(3,4);
    P p5 = P(3,5);
    vector<P> ve;
    ve.push_back(p3);
    ve.push_back(p2);
    ve.push_back(p1);
    ve.push_back(p4);
    ve.push_back(p5);
    sort(ve.begin(),ve.end());
    for(int i=0;i<ve.size();i++){
        cout<<ve[i].first<<" "<<ve[i].second<<endl;
    }
    return 0;
}

结果:

当然,你也可以自定义比较级。利用sort的回调函数cmp就可以实现。

现在实现一个根据第一个关键字从大到小,如第一关键字相等,第二关键字从小到大的排序方式。


bool cmp(P p1,P p2){
    if(p1.first==p2.first)return p1.second<p2.second;
    else return p1.first>p2.first;
}

int main() {
    P p1 = P(1,3);
    P p2 = P(2,2);
    P p3 = P(3,1);
    P p4 = P(3,4);
    P p5 = P(3,5);

    vector<P> ve;
    ve.push_back(p3);
    ve.push_back(p2);
    ve.push_back(p1);
    ve.push_back(p4);
    ve.push_back(p5);

    sort(ve.begin(),ve.end(),cmp);
    for(int i=0;i<ve.size();i++){
        cout<<ve[i].first<<" "<<ve[i].second<<endl;
    }

    return 0;
}

3.pair与其他STL的结合

pair是一个可以哈希的结构,这意味着我们可以把一个piar当成一个唯一的值,就可以巧妙的与map结合使用。

如:判定给出的坐标中,有多少个不同的点。(1,2)(1,3)(1,4)(1,2)(1,3)

这里使用了typedef pair<int,int> P

int main() {
    P p1 = P(1,3);
    P p2 = P(1,2);
    P p3 = P(1,4);
    P p4 = P(1,2);
    P p5 = P(1,3);
    map<P,int>ma;
    ma[p1] = ma[p1] + 1;
    ma[p2] = ma[p2] + 1;
    ma[p3] = ma[p3] + 1;
    ma[p4] = ma[p4] + 1;
    ma[p5] = ma[p5] + 1;
    auto index = ma.begin();
    cout<<ma.size()<<endl;
    return 0;
}

当然,也可以用于set的创建

int main() {
    P p1 = P(1,3);
    P p2 = P(1,2);
    P p3 = P(1,4);
    P p4 = P(1,2);
    P p5 = P(1,3);
    set<P>se;
    se.insert(p1);
    se.insert(p2);
    se.insert(p3);
    se.insert(p4);
    se.insert(p5);
    cout<<se.size()<<endl;
    return 0;
}

由于set自动去重的性质,直接输出size即可。

ps:某些时候还是声明结构体好用

03-08
### C++ 中 `pair` 的用法 在 C++ 标准库中,`std::pair` 是一种用于存储两个不同类型数据的容器模板。通过使用 `std::make_pair()` 函数可以方便地创建一对值。 #### 创建和初始化 `pair` 可以直接定义并初始化一个 `pair`: ```cpp #include <utility> // For std::pair and std::make_pair #include <iostream> int main() { // 定义并初始化 pair std::pair<int, double> p1(10, 3.14); // 使用 make_pair 初始化 std::pair<std::string, int> p2 = std::make_pair(std::string("hello"), 42); // 输出 pairs std::cout << "Pair 1: (" << p1.first << ", " << p1.second << ")" << std::endl; std::cout << "Pair 2: (" << p2.first << ", " << p2.second << ")" << std::endl; return 0; } ``` #### 访问 `pair` 成员 可以通过 `.first` 和 `.second` 来访问 `pair` 中的第一个和第二个成员[^1]。 #### 遍历 map 或 multimap 当遍历像 `std::map` 这样的关联容器时,迭代器指向的是键值对类型的元素——即 `std::pair<const Key, T>` 类型的对象。因此,在遍历时可以直接解构这些对象以便于操作其内部的数据。 ```cpp #include <map> #include <iostream> void traverseMap(const std::map<int, std::string>& my_map) { for (const auto& pair : my_map) { std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl; } } int main() { std::map<int, std::string> example{{1, "one"}, {2, "two"}}; traverseMap(example); return 0; } ``` 上述代码展示了如何利用范围 for 循环来遍历 `std::map` 并打印其中每项的键和对应的值。 #### 可变参数宏中的注意事项 需要注意的是,在某些情况下(比如编写可变参数宏),如果涉及到 `__VA_OPT__` 关键字,则应遵循特定规则以避免编译错误。例如,C++ 不允许在可变参数宏体外的地方使用此关键字[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值