set_union 및 set_intersection - C ++에 문제가 발생합니다
-
26-12-2019 - |
문제
현재 계산을 포함하는 프로젝트에서 일하고 있습니다.나는 set_union 및 set_intersection 함수를 사용하여 집합의 유니온 및 교차로를 계산합니다.내 변수는 다음과 같습니다.
int AunionB[8];
int AunionC[7]; // define AunionC
int BunionD[9]; // define BunionD
int AintersectB[4]; // define AintersectB
int AintersectC[3]; // define AintersectC
set<int> finalUnion; // final union of A, B, C, D
set<int> finalIntersection; // final intersection of A, B, C, D
.
이들은 각각 4 세트, Seta, Setb, Setc 및 Setd의 4 세트의 노조 및 교차점입니다.my set_union 함수는 다음과 같습니다.
set_union(AunionC, AunionC+7, BunionD, BunionD+9, finalUnion.begin());
.
및 my set_intersection 함수는 다음과 같습니다.
set_intersection(AintersectB, AintersectB+4, AintersectC,
AintersectC+3, finalIntersection.begin());
.
내가 컴파일 할 때, 나는 "여기에서 필요로하는"오류를 얻을 수 있습니다.누군가가 이것을 도와 줄 수 있습니까?나는 그것이 set_union 및 set_intersection 함수와 해당 매개 변수와 관련이 있지만 확실하지는 않습니다.
해결책
대신
를 사용하십시오#include <iterator>
//...
set_union( AunionC, AunionC+7, BunionD, BunionD+9,
std::inserter( finalUnion, finalUnion.begin() ) );
.
및
set_intersection(AintersectB, AintersectB+4, AintersectC,
AintersectC+3, std::inserter( finalIntersection, finalIntersection.begin() ) );
.
시범 예시
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>
int main()
{
int a[] = { 1, 3, 5 };
int b[] = { 0, 2, 4, 6 };
std::set<int> s;
std::set_union( std::begin( a ), std::end( a ),
std::begin( b ), std::end( b ),
std::inserter( s, s.begin() ) );
for ( int x : s ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
.
출력은
0 1 2 3 4 5 6
. 제휴하지 않습니다 StackOverflow