C ++ programming-& gt; pair (peer group), programming pair

Source: Internet
Author: User

C ++ programming-> pair (peer group), programming pair

Pair is a template type. Each pair can store two values. These two values are not limited. They can be tuple, vector, string, struct, and so on.

First, let's look at the pair function.

Initialization, replication, and other related operations are as follows:

Default (1)
constexpr pair();
Copy/move (2)
template<class U, class V> pair (const pair<U,V>& pr);template<class U, class V> pair (pair<U,V>&& pr);pair (const pair& pr) = default;pair (pair&& pr) = default;
Initialization (3)
pair (const first_type& a, const second_type& b);template<class U, class V> pair (U&& a, V&& b);
Piecewise (4)
template <class... Args1, class... Args2>  pair (piecewise_construct_t pwc, tuple<Args1...> first_args,                                   tuple<Args2...> second_args);

// Pair template functions // exchange function template <class _ Ty1, class _ Ty2> inlinevoid swap (pair <_ Ty1, _ Ty2> & _ Left, pair <_ Ty1, _ Ty2> & _ Right) {// swap _ Left and _ Right pairs_Left.swap (_ Right) ;}// judge whether the function template is equal <class _ Ty1, class _ Ty2> inlinebool operator = (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test for pair equalityreturn (_ Left. first = _ Right. first & _ Left. second = _ Right. second); // Both elements are compared} // determines whether the unequal function template <class _ Ty1, class _ Ty2> inlinebool operator! = (Const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test for pair inequalityreturn (! (_ Left = _ Right);} // judge whether it is smaller than the function template <class _ Ty1, class _ Ty2> inlinebool operator <(const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left <_ Right for pairsreturn (_ Left. first <_ Right. first |! (_ Right. first <_ Left. first) & _ Left. second <_ Right. second);} // determine whether the value is greater than the function template <class _ Ty1, class _ Ty2> inlinebool operator> (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left> _ Right for pairsreturn (_ Right <_ Left );} // determine whether the value is less than or equal to the template <class _ Ty1, class _ Ty2> inlinebool operator <= (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left <= _ Righ T for pairsreturn (! (_ Right <_ Left);} // judge whether the value is greater than or equal to the template of the function <class _ Ty1, class _ Ty2> inlinebool operator >=( const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left> = _ Right for pairsreturn (! (_ Left <_ Right ));}

Paste a piece of code:

// Pair defines pair <string, int> pair1; // pair defines and assigns a value to a pair <string, int> pair2 ("lily", 4); pair <string, int> pair3 (pair2); // pair value assignment method 2 pair1 = make_pair (string ("tom"), 3); // pair value assignment method 3 pair1.first = "jim "; pair1.second = 2; // pair value assignment method four get <0> (pair1) = string ("jim"); get <1> (pair1) = 6; // pair value Assignment Method 5 swap (pair1, pair3); // pair output mode 1 cout <pair2.first <endl; cout <pair2.second <endl; // pair Output Mode 2: cout <get <0> (pair1) <endl; cout <get <1> (pair1) <endl;



C language programming, using the selection method to sort 20 real numbers in the array

Select sorting method:

# Include "stdio. h"
# Define N 20
Main ()
{
Int I, j, k;
Float a [N], t;
Printf ("Please enter 20 digits: \ n ");
For (I = 0; I <N; I ++)
Scanf ("% f", & a [I]);
For (I = 0; I <N-1; I ++)
{
K = I;
For (j = I + 1; j <N; j ++)
If (a [k]> a [j]) k = j;
If (I! = K)
{
T = a [k];
A [k] = a [I];
A [I] = t;
}
}
For (I = 0; I <N; I ++)
Printf ("% f \ t", a [I]);
Printf ("\ n ");
}

Insert sorting method:

# Include "stdio. h"
# Define N 20
Main ()
{
Int I, j;
Float a [N], k;
Printf ("Please enter 20 digits: \ n ");
For (I = 0; I <N; I ++)
Scanf ("% f", & a [I]);
For (I = 0; I <N; I ++)
{
K = a [I];
J = I-1;
While (j> = 0 & k <a [j])
{
A [j + 1] = a [j];
J --;
}
A [j + 1] = k;
}
For (I = 0; I <N; I ++)
Printf ("% f \ t", a [I]);
Printf ("\ n ");
}

Bubble sorting method:

# Include "stdio. h"
# Define N 20
Main ()
{
Int I, j;
Float a [N], t;
Printf ("Please enter 20 digits: \ n ");
For (I = 0; I <N; I ++)
Scanf ("% f", & a [I]);
For (I = 0; I <N; I ++)
For (j = I + 1; j <N; j ++)
If (a [I]> a [j])
{
T = a [I];
A [I] = a [j];
A [j] = t;
}
For (I = 0; I <N; I ++)
Printf ("% f \ t", a [I]);
Printf ("\ n ");
}

If it is helpful to you, please remember to adopt the best answer. Thank you !... Remaining full text>

C language programming: there is a one-dimensional array with 10 student scores, write a function, and the average score, the highest score and the lowest score

# Include <stdio. h>
Float aveg (int a []);
Int max (int a []);
Int min (int a []);

Float aveg (int a [])
{Int I, sum = 0;
Float av;
For (I = 0; I <= 9; I ++)
{
Sum = sum + a [I];

}
Printf ("% d \ n", sum );
Av = sum/10.00;
Return av;
}
Int min (int a [])
{Int imin, I;
Imin = a [0];
For (I = 0; I <= 9; I ++)
{If (imin> a [I])
Imin = a [I];
}
Return imin;
}

Int max (int a [])
{Int imax, I;
Imax = a [0];
For (I = 0; I <= 9; I ++)
{If (imax <a [I])
Imax = a [I];
}
Return imax;
}
Void main ()
{
Int a [10], I, imax, imin;
Float av;
Printf ("input 10 scores: \ n ");
For (I = 0; I <10; I ++)
{Scanf ("% d", & a [I]);}
Av = aveg ();
Imax = max ();
Imin = min ();
Printf ("% f \ n % d \ n", av, imax, imin );
}
QQ group: 2326077

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.