logo

C++ 표준 템플릿 라이브러리(STL)의 쌍

쌍은 서로 다른 데이터 유형일 수 있는 두 값을 결합하는 데 사용됩니다. 쌍은 두 개의 이종 객체를 단일 단위로 저장하는 방법을 제공합니다. 기본적으로 튜플을 저장하려는 경우에 사용됩니다. 쌍 컨테이너는 다음에 정의된 간단한 컨테이너입니다. 두 개의 데이터 요소 또는 개체로 구성된 헤더입니다.

  • 첫 번째 요소는 'first'로, 두 번째 요소는 'second'로 참조되며 순서는 고정되어 있습니다(첫 번째, 두 번째).
  • 쌍을 할당, 복사 및 비교할 수 있습니다. 할당된 객체의 배열 지도 또는 hash_map은 기본적으로 모든 '첫 번째' 요소가 '두 번째' 값 개체와 연결된 고유 키인 '쌍' 유형입니다.
  • 요소에 액세스하려면 변수 이름, 도트 연산자, 키워드 first 또는 second를 사용합니다.

통사론:



pair Pair_name>

CPP






// CPP program to illustrate Pair in STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >// defining a pair> >pair<>int>,>char>>쌍1;> >// first part of the pair> >PAIR1.first = 100;> >// second part of the pair> >PAIR1.second =>'G'>;> >cout << PAIR1.first <<>' '>;> >cout << PAIR1.second << endl;> >return> 0;> }>



>

>

산출

100 G>

쌍 초기화: 쌍을 초기화할 수도 있습니다.

통사론:

pair Pair_name (value1, value2) ;>

쌍을 초기화하는 다양한 방법:

pair g1; //default pair g2(1, 'a'); //initialized, different data type pair g3(1, 10); //initialized, same data type pair g4(g3); //copy of g3>

쌍을 초기화하는 또 다른 방법은 make_pair() 함수를 사용하는 것입니다.

g2 = make_pair(1, 'a');>

쌍을 선언하는 또 다른 유효한 구문은 다음과 같습니다.

g2 = {1, 'a'};>

CPP

비교 메소드 자바




// CPP program to illustrate> // Initializing of pair STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >// defining a pair> >pairdouble>PAIR2('GeeksForGeeks', 1.23); 시합<< PAIR2.first << ' '; cout << PAIR2.second << endl; return 0; }>

>

>

산출

GeeksForGeeks 1.23>

메모: 초기화되지 않은 경우 쌍의 첫 번째 값이 자동으로 초기화됩니다.

C++




// CPP program to illustrate> // auto-initializing of pair STL> #include> #include> using> namespace> std;> int> main()> {> >pair<>int>,>double>>쌍1;> >pairchar>쌍2; // 0으로 초기화됨 cout<< PAIR1.first; // it is initialised to 0 cout << PAIR1.second; cout << ' '; // // it prints nothing i.e NULL cout << PAIR2.first; // it prints nothing i.e NULL cout << PAIR2.second; return 0; }>

>

>

산출:

00>

회원 기능

1) make_pair() : 이 템플릿 함수를 사용하면 유형을 명시적으로 작성하지 않고도 값 쌍을 만들 수 있습니다.
통사론:

Pair_name = make_pair (value1,value2);>

CPP


C의 포인터



// CPP Program to demonstrate make_pair()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>int>,>char>>쌍1;> >pairdouble>PAIR2('GeeksForGeeks', 1.23); 페어더블> PAIR3; PAIR1.first = 100; PAIR1.초 = 'G'; PAIR3 = make_pair('GeeksForGeeks가 최고입니다', 4.56); 시합<< PAIR1.first << ' '; cout << PAIR1.second << endl; cout << PAIR2.first << ' '; cout << PAIR2.second << endl; cout << PAIR3.first << ' '; cout << PAIR3.second << endl; return 0; }>

>

>

산출

100 G GeeksForGeeks 1.23 GeeksForGeeks is Best 4.56>

2) 교환: 이 함수는 한 쌍 개체의 내용을 다른 쌍 개체의 내용으로 바꿉니다. 쌍은 동일한 유형이어야 합니다.
통사론:

캣 팀프 순 가치
pair1.swap(pair2) ;>

동일한 유형의 pair1과 pair2라고 하는 두 개의 지정된 쌍에 대해 스왑 함수는 pair1.first를 pair2.first로, pair1.second를 pair2.second로 바꿉니다.

CPP




// CPP Program to demonstrate swap()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>char>,>int>>pair1 = make_pair(>'A'>, 1);> >pair<>char>,>int>>pair2 = make_pair(>'B'>, 2);> >cout <<>'Before swapping: '>;> >cout <<>'Contents of pair1 = '> << pair1.first <<>' '> ><< pair1.second;> >cout <<>'Contents of pair2 = '> << pair2.first <<>' '> ><< pair2.second;> >pair1.swap(pair2);> >cout <<>' After swapping: '>;> >cout <<>'Contents of pair1 = '> << pair1.first <<>' '> ><< pair1.second;> >cout <<>'Contents of pair2 = '> << pair2.first <<>' '> ><< pair2.second;> >return> 0;> }>

>

>

산출

Before swapping: Contents of pair1 = A 1Contents of pair2 = B 2 After swapping: Contents of pair1 = B 2Contents of pair2 = A 1>

3) 넥타이(): 이 기능은 다음과 동일하게 작동합니다. 튜플 . 즉, 튜플(또는 여기서 쌍) 값을 별도의 변수로 압축을 풀기 위해 인수에 대한 lvalue 참조의 튜플을 만듭니다. 튜플과 마찬가지로 여기에는 무시가 있는 것과 없는 것의 두 가지 변형 변형이 있습니다. ignore 키워드는 특정 튜플 요소가 압축 해제되는 것을 무시합니다.
그러나 튜플에는 여러 인수가 있을 수 있지만 쌍에는 인수가 두 개만 있습니다. 따라서 쌍 쌍의 경우 언패킹을 명시적으로 처리해야 합니다.
통사론:

tie(int &, int &) = pair1;>

CPP




// CPP code to illustrate tie() in Pair> #include> using> namespace> std;> // Driver Code> int> main()> {> >pair<>int>,>int>>pair1 = { 1, 2 };> >int> a, b;> >tie(a, b) = pair1;> >cout << a <<>' '> << b <<>' '>;> >pair<>int>,>int>>pair2 = { 3, 4 };> >tie(a, ignore) = pair2;> >// prints old value of b> >cout << a <<>' '> << b <<>' '>;> >// Illustrating pair of pairs> >pair<>int>, pair<>int>,>char>>> 쌍3 = { 3, { 4,>'a'> } };> >int> x, y;> >char> z;> >// tie(x,y,z) = pair3; Gives compilation error> >// tie(x, tie(y,z)) = pair3; Gives compilation error> >// Each pair needs to be explicitly handled> >tie(x,ignore) = pair3;> >tie(y, z) = pair3.second;> >cout << x <<>' '> << y <<>' '> << z <<>' '>;> }> // contributed by sarthak_eddy.>

>

>

산출

1 2 3 2 3 4 a>

쌍으로 된 함수를 설명하는 코드:

CPP




CSS를 사용하여 밑줄
// CPP program to illustrate pair in STL> #include> #include> #include> using> namespace> std;> int> main()> {> >pairint>g1; pairint> g2('퀴즈', 3); pairint> g3(g2); 쌍 g4(5, 10); g1 = make_pair(string('Geeks'), 1); g2.first = '.com'; g2.초 = 2; 시합<< 'This is pair g' << g1.second << ' with ' << 'value ' << g1.first << '.' << endl << endl; cout << 'This is pair g' << g3.second << ' with value ' << g3.first << 'This pair was initialized as a copy of ' << 'pair g2' << endl << endl; cout << 'This is pair g' << g2.second << ' with value ' << g2.first << ' The values of this pair were' << ' changed after initialization.' << endl << endl; cout << 'This is pair g4 with values ' << g4.first << ' and ' << g4.second << ' made for showing addition. The ' << 'sum of the values in this pair is ' << g4.first + g4.second << '.' << endl << endl; cout << 'We can concatenate the values of' << ' the pairs g1, g2 and g3 : ' << g1.first + g3.first + g2.first << endl << endl; cout << 'We can also swap pairs ' << '(but type of pairs should be same) : ' << endl; cout << 'Before swapping, ' << 'g1 has ' << g1.first << ' and g2 has ' << g2.first << endl; swap(g1, g2); cout << 'After swapping, ' << 'g1 has ' << g1.first << ' and g2 has ' << g2.first; return 0; }>

>

>

산출

This is pair g1 with value Geeks. This is pair g3 with value QuizThis pair was initialized as a copy of pair g2 This is pair g2 with value .com The values of this pair were changed after initialization. This is pair g4 with values 5 and 10 made for showing addition. The sum of the values in this pair is 15. We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com We can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .com After swapping, g1 has .com and g2 has Geeks>

시간 복잡도: 오(1).

보조 공간: 오(1).

쌍의 연산자(=, ==, !=,>=, <=)

연산자를 쌍으로 사용할 수도 있습니다.

1) 같음(=) 사용: 쌍 개체에 대한 새 개체를 할당합니다. 통사론:

pair& operator= (const pair& pr);>

이는 쌍 개체의 새 콘텐츠로 pr을 할당합니다. 첫 번째 값에는 pr의 첫 번째 값이 할당되고 두 번째 값에는 pr의 두 번째 값이 할당됩니다.

2) 쌍이 있는 비교(==) 연산자: pair1과 pair2라는 주어진 두 쌍에 대해 비교 연산자는 두 쌍의 첫 번째 값과 두 번째 값을 비교합니다. 즉, pair1.first가 pair2.first와 같은지 여부와 pair1.second가 pair2.second와 같은지 여부입니다. .

즉, if ( (pari1.first ==pair2.first) && (pair1.second==pair2.second) )

두 조건 중 하나라도 false이면 false를 반환하고 그렇지 않으면 true를 반환합니다.

3) 쌍이 있는 같지 않음(!=) 연산자: pair1과 pair2라는 주어진 두 쌍에 대해 != 연산자는 두 쌍의 첫 번째 값을 비교합니다. 즉, pair1.first가 pair2.first와 같거나 같지 않으면 두 쌍의 두 번째 값을 확인합니다.

4) 쌍이 있는 논리(>=, <= ) 연산자: 주어진 두 쌍에 대해 pair1과 pair2라고 하면 =,>를 쌍에도 사용할 수 있습니다. 쌍의 첫 번째 값만 비교하여 0 또는 1을 반환합니다. p1=(1,20) 및 p2=(1,10)과 같은 쌍의 경우 p2또는

&list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB