이 코드에 문제가 있습니다. 코드가 컴파일되고 있지만 벡터의 내용을 테스트하려고하면 테스트가 실패합니다. 여기C++ set_union iterator
using std::vector;
// returns a different vector, containing all elements in v1 and all elements in v2 (elements who are either in v1 or v2) but no duplicates.
template <typename T> vector<T> set_union(const vector<T>& v1, const vector<T>&v2)
{
vector<T> v(20);
typename vector<T>::iterator it;
it = set_union (v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
return v;
}
내가 실행하고 테스트한다 : 나는이 테스트를 실행하면
TEST_F(MyTest,set_union) {
vector<int> v1{1,3,2};
vector<int> v2{1,4};
vector<int> v=set_union(v1,v2);
ASSERT_EQ(0,count(v,9));
ASSERT_EQ(1,count(v,1));
ASSERT_EQ(1,count(v,2));
ASSERT_EQ(1,count(v,3));
ASSERT_EQ(1,count(v,4));
이
는, 첫 번째 테스트를 통과},하지만 두 번째 테스트 0을 반환 여기에 코드입니다 벡터에서 숫자 1의 인스턴스. 여기서 응답은 1 인스턴스 여야합니다.
결과에 미리 정의 된 크기의 벡터를 사용하면 안됩니다. 대신에'std :: back_inserter (v)'를 사용하십시오. 결과의 크기를 잘 알고 있다면'v.reserve()'를 원할 수있다. –
@ DietmarKühl : 출력 크기의 상한 및 하한은 각각 '최대 (v1.size(), v2.size())'및 'v1.size() + v2.size()'입니다. –