C++pair to group __c++

Source: Internet
Author: User

Pair to group:

Pair is equivalent to a structure with two elements


Call header file:

#include <algorithm>

using namespace Std;


Detailed usage (part):

pair<string, double> p2------P2 has two types, the first element is string, and the second element is double

P2 = Make_pair ("AA", 5.0)------for P2 assignment

Pair<string, double> p3 (p2)------pair Copy

P2.first------Access the first element in pair

P2.second------Access to the second element in pair


Sort:

BOOL Comp (pair<int, int> A, pair<int, int> b)

{ ...... }

If there is no write-sort function, the first element is sorted by default, if the same is sorted by the second element (small to large)


#include <stdio.h>
#include <algorithm>
using namespace std;
Pair<int, int> p[15];
BOOL Comp (pair<int, int> A, pair<int, int> b)
{
	if (A.first>b.first | | A.first==b.first & & A.second>b.second) return
		1;
	return 0;
}
int main (void)
{
	int i, j, k = 0;
	for (i=3;i>=1;i--)
	{
		for (j=1;j<=4;j++)
			p[++k] = Make_pair (i, j);
	}
	Sort (p+1, p+k+1);
	for (i=1;i<=k;i++)
		printf ("%d%d\n", P[i].first, P[i].second);
	printf ("\ n");
	Sort (p+1, p+k+1, comp);
	for (i=1;i<=k;i++)
		printf ("%d%d\n", P[i].first, P[i].second);
	return 0;
}


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.