当函数的返回值不只一个时,返回类型可以有数组,链表等等,但是,这些都有一个限制,即返回类型必须相同,当返回类型不同时,可以用结构体、类、pair,pair相比前两个的优势为,pair是系统定义好的类,但返回值为两个时,使用pair比使用结构体和类更具有优势
首先,看下定义
可以跳过定义直接看后面
template<class _Ty1,class _Ty2>
struct pair
{ // store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;
pair()
: first(), second()
{ // default construct
}
pair(const _Ty1& _Val1, const _Ty2& _Val2)
: first(_Val1), second(_Val2)
{ // construct from specified values
}
template<class _Other1,
class _Other2,
class = typename enable_if<is_convertible<const _Other1&, _Ty1>::value
&& is_convertible<const _Other2&, _Ty2>::value,
void>::type>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
template<class _Other1,
class _Other2>
_Myt& operator=(const pair<_Other1, _Other2>& _Right)
{ // assign from compatible pair
first = _Right.first;
second = _Right.second;
return (*this);
}
template<class _Tuple1,
class _Tuple2,
size_t... _Indexes1,
size_t... _Indexes2> inline
pair(_Tuple1& _Val1,
_Tuple2& _Val2,
_Arg_idx<_Indexes1...>,
_Arg_idx<_Indexes2...>);
template<class... _Types1,
class... _Types2> inline
pair(piecewise_construct_t,
tuple<_Types1...> _Val1,
tuple<_Types2...> _Val2)
_NOEXCEPT_OP((is_nothrow_constructible<_Ty1, _Types1&&...>::value
&& is_nothrow_constructible<_Ty2, _Types2&&...>::value));
template<class _Other1,
class _Other2,
class = typename enable_if<is_convertible<_Other1, _Ty1>::value
&& is_convertible<_Other2, _Ty2>::value,
void>::type>
pair(_Other1&& _Val1, _Other2&& _Val2)
_NOEXCEPT_OP((is_nothrow_constructible<_Ty1, _Other1&&>::value
&& is_nothrow_constructible<_Ty2, _Other2&&>::value))
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
template<class _Other1,
class _Other2,
class = typename enable_if<is_convertible<_Other1, _Ty1>::value
&& is_convertible<_Other2, _Ty2>::value,
void>::type>
pair(pair<_Other1, _Other2>&& _Right)
_NOEXCEPT_OP((is_nothrow_constructible<_Ty1, _Other1&&>::value
&& is_nothrow_constructible<_Ty2, _Other2&&>::value))
: first(_STD forward<_Other1>(_Right.first)),
second(_STD forward<_Other2>(_Right.second))
{ // construct from moved compatible pair
}
template<class _Other1,
class _Other2>
_Myt& operator=(pair<_Other1, _Other2>&& _Right)
_NOEXCEPT_OP((is_nothrow_assignable<_Ty1, _Other1&&>::value
&& is_nothrow_assignable<_Ty2, _Other2&&>::value))
{ // assign from moved compatible pair
first = _STD forward<_Other1>(_Right.first);
second = _STD forward<_Other2>(_Right.second);
return (*this);
}
_Myt& operator=(_Myt&& _Right)
_NOEXCEPT_OP((is_nothrow_move_assignable<_Ty1>::value
&& is_nothrow_move_assignable<_Ty2>::value))
{ // assign from moved pair
first = _STD forward<_Ty1>(_Right.first);
second = _STD forward<_Ty2>(_Right.second);
return (*this);
}
void swap(_Myt& _Right)
_NOEXCEPT_OP(_NOEXCEPT_OP(_Swap_adl(this->first, _Right.first))
&& _NOEXCEPT_OP(_Swap_adl(this->second, _Right.second)))
{ // exchange contents with _Right
if (this != &_Right)
{ // different, worth swapping
_Swap_adl(first, _Right.first);
_Swap_adl(second, _Right.second);
}
}
_Myt& operator=(const _Myt& _Right)
{ // assign from copied pair
first = _Right.first;
second = _Right.second;
return (*this);
}
_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
};
从类的定义可以看出
pair有两个属性,分别是first,second
_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
pair的两个属性的类型,是声明pair时候输入的
如:
pair<string ,int> temp_pair;
temp_pair.first的类型为string
temp_pair.second的类型为int
说了那么多,举个栗子吧!
#include<iostream>
#include<string>
using namespace std;
pair<string, int> fun_test_pair(string str, int i_a)
{
pair<string, int> pair_p;
pair_p.first = str;
pair_p.second = i_a;
return pair_p;
}
int main()
{
string s_a("hello");
int i_a = 2;
pair<string, int> pair_a;
pair_a = fun_test_pair(s_a, i_a);
cout << pair_a.first << " " << pair_a.second << endl;
return 0;
}
是不是觉得定义pair的时候很麻烦,我也这么觉得,c艹 提供了我们一种偷懒的方法—–typedef
typedef pair<string ,int> pair_si;
刚才的代码可以简化为:
#include<iostream>
#include<string>
using namespace std;
typedef pair<string, int> pair_si;
pair_si fun_test_pair(string str, int i_a)
{
pair_si pair_p;
pair_p.first = str;
pair_p.second = i_a;
return pair_p;
}
int main()
{
string s_a("hello");
int i_a = 2;
pair_si pair_a;
pair_a = fun_test_pair(s_a, i_a);
cout << pair_a.first << " " << pair_a.second << endl;
return 0;
}
只能返回两个参数是不是觉得有些遗憾呢?继续往后看
#include<iostream>
#include<string>
using namespace std;
typedef pair<string, pair<double,int> > pair_sdi;
typedef pair<double, int > pair_di;
pair_sdi fun_test_pair(string str, double d_a, int i_a)
{
pair_di pair_di_p;
pair_di_p.first = d_a;
pair_di_p.second = i_a;
pair_sdi pair_sdi_p;
pair_sdi_p.first = str;
pair_sdi_p.second = pair_di_p;
return pair_sdi_p;
}
int main()
{
string s_a("hello");
int i_a = 2;
double d_a = 3.2;
pair_sdi pair_sdi_a;
pair_sdi_a = fun_test_pair(s_a, d_a, i_a);
cout << pair_sdi_a.first << " " << pair_sdi_a.second.first <<" "<< pair_sdi_a.second.second<< endl;
return 0;
}
pair<string ,pair<double ,int> >
最后两个>> 之间要有空格,string 可以是自己定义的结构体、类等任意类型
上述代码的typedef可以修改为
修改前:
typedef pair<string, pair<double, int> > pair_sdi;
typedef pair<double, int > pair_di;
修改后
typedef pair<double, int > pair_di;
typedef pair<string, pair_di> pair_sdi;