C++ std::pair

  • 重点总结:
  • pair将2个数据组合成一组数据,std::map中的元素就是pair。
  • 通过 first 和 sencond 访问pair中数据:pair.first; air.second;
  • 可使用 typedef 简化声明,如:1:typedef pair<string, int> Student;
                  2:Student pair_temp("fufu",12);


1. 构造函数

  • 头文件#include <utility>
  • <1>. 构造函数有以下几种类型,函数原型如下:
constexpr pair();										// 1. 默认构造函数

template<class U, class V> pair(const pair<U,V>& pr);	// 2. Copy/Move 构造函数
template<class U, class V> pair(pair<U,V>&& pr);
pair(const pair& pr) = default;
pair(pair&& pr) = default;

pair(const first_type& a, const second_type& b);		// 3. 初始化构造函数
template<class U, class V> 
	pair(U&& a, V&& b);

template <class... Args1, class... Args2>				// 4. 分段构造函数
pair(piecewise_construct_t pwc, tuple<Args1...> first_args,
                                 tuple<Args2...> second_args);

例-代码如下:

#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
	std::pair<std::string,double> product1;                     	// [1].默认构造, 元素初始化为默认值如 int:0
	std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
	
	std::pair<std::string,double> product2("tomatoes", 2.30);   	// [3].初始化构造
	std::pair<std::string,double> product3(product2);          		// [2].copy
	product1 = std::make_pair(std::string("lightbulbs"), 0.99);   	// [2].move
	
	product2.first = "shoes";                  // 其中 first 对应 string
	product2.second = 39.90;                   // 其中 second 对应 double
	
	std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
	std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
	std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
	return 0;

	// 打印结果:
    // The price of  is $0
    // The price of lightbulbs is $0.99
    // The price of shoes is $39.9
    // The price of tomatoes is $2.3
}

2. 重载函数 operator=

  • operator=函数原型如下:
pair& operator= (const pair& pr);				// 1. copy
template <class U, class V>
  	pair& operator= (const pair<U,V>& pr);

pair& operator= (pair&& pr)						// 2. move
           noexcept (is_nothrow_move_assignable<first_type>::value &&
                     is_nothrow_move_assignable<second_type>::value);                 
template <class U, class V>
pair& operator= (pair<U,V>&& pr);

例子:

int main () {
	std::pair <std::string,int> planet, homeplanet;
	
	planet = std::make_pair("Earth",6371);			// move 移动赋值
	homeplanet = planet;							// copy 复制赋值
	
	std::cout << "Home planet: " << homeplanet.first << '\n';
	std::cout << "Planet size: " << homeplanet.second << '\n';
	return 0;

	// 打印结果:
	// Home planet: Earth
	// Planet size: 6371
}

3. 成员函数 - swap()

  • std::pair::swap,将pair对象的内容与参数pr的内容交换,其原型如下所示:
template <class T> void swap (T& a, T& b)						// 1. non-array
  noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value);

template <class T, size_t N> void swap(T (&a)[N], T (&b)[N])	// 2. array 
  noexcept (noexcept(swap(*a,*b)));

// The behavior of these function templates is equivalent to: 具体实现
template <class T> void swap (T& a, T& b) {
  T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T &a[N], T &b[N]) {
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}

1. 成员函数std::pair::swap例子:

int main () {
    std::pair<int,char> foo(10,'a');
    std::pair<int,char> bar(90,'z');

    foo.swap(bar);
    std::cout << "foo contains: " << foo.first << " " << foo.second << '\n';

    return 0;	// 打印:foo contains: 90 z
}

2. 标准库中的std::swap()例子:

int main () {
    std::pair<int,char> foo(10,'a');
    std::pair<int,char> bar(90,'z');

    swap(foo,bar);
    std::cout << "foo contains: " << foo.first << " " << foo.second << '\n';

    return 0;	// 打印:foo contains: 90 z
    			// 两中方式进行swap,效果相同
}

4. make_pair()

  • make_pair()构造pair对象,其中第一个元素设为x、第二个元素设为y。
  • pair对象可以从包含不同类型的其他pair对象构造,前提是对应的类型可以隐式转换。原型如下:
template <class T1, class T2>
pair<V1,V2> make_pair(T1&& x, T2&& y);

    例子:

int main () {
	std::pair<int,int> foo;
	std::pair<int,int> bar;
	
	foo = std::make_pair(10,20);
	bar = std::make_pair(10.5,'A'); 	// pair<double,char> 隐式转换 --> pair<int,int>
	
	std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
	std::cout << "bar: " << bar.first << ", " << bar.second << '\n';
	
	return 0;
	
	// 打印结果:
	// foo: 10, 20
	// bar: 10, 65     
}

5. pair的排序规则

  • 当容器中元素为std::pair时,pair默认比较其first元素的大小,当first元素相同时,再比较second元素的大小。
int main() {
    typedef std::pair<int,string> Pair_temp;

    vector<Pair_temp> vector_;

    vector_.emplace_back(Pair_temp(1,"aa"));
    vector_.emplace_back(Pair_temp(2,"bb"));
    vector_.emplace_back(Pair_temp(2,"auau"));
    vector_.emplace_back(Pair_temp(3,"mimi"));
    

    sort(vector_.begin(),vector_.end());

    for (auto& iter:vector_)
        cout << "Index:" << iter.first << ", name:" << iter.second << endl;
    return 0;

	// 打印结果:
	// Index:1, name:aa
	// Index:2, name:auau
	// Index:2, name:bb
	// Index:3, name:mimi
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值