집합의 모든 요소를 이전 요소와 비교하려는 기능이 있습니다. 나는 이런 식으로 뭔가를 할 :순방향 반복기를 사용하여 반복 반복
std::set<int> sSet;
std::set<int>::iterator it;
std::set<int>::iterator itR;
sSet.insert(1);
sSet.insert(2);
sSet.insert(3);
sSet.insert(4);
sSet.insert(5);
sSet.insert(6);
for (it=sSet.begin(); it!=sSet.end(); ++it) //simple forward loop
{
itR = it;
if(it != sSet.begin())
itR--;
for(;itR!=sSet.begin();itR--)
{
//Reverse iteration
//for comparing every element to all the previous elements
//Problem here is it goes up to the second element only and not first
//because of the condition itR!=sSet.begin()
}
}
내가 여기에 역 반복자를 사용하여 생각했지만 다시 내가 특정 위치 (또는 앞으로 반복자)에서 역 반복자를 설정하는 방법을 찾을 수 없습니다.
올바른 방법이 있습니까?
업데이트 : 위에 사용 된 세트는 데모 용입니다. 클래스와 세트로 실제 구현은 아래와 같이 정의된다 : 루프 동안
std::set<TBigClass, TBigClassComparer> sSet;
class TBigClassComparer
{
public:
bool operator()(const TBigClass s1, const TBigClass s2) const
{
//comparison logic goes here
}
};
itR = it; 이것은 유효한 지정이 아닙니다. – skmic
@skmic, 예 감사합니다. 나는 답을 편집했다. 나는 reverse_iterator가 어떻게 사용되어야 하는지를 바꿨다. 설명을 주셔서 감사합니다. –
. 어디에서 초기화 했니? 나는이 선 'itR = it;을 제거하고 싶다고 가정합니다. 왜냐하면 컴파일 에러를 줄 것이기 때문이다. 그래서 itR은 현재 null ptr입니다. – skmic