달력

42019  이전 다음

  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  •  
  •  
  •  
  •  

std::remove_if ....

삽질 2011.11.06 15:00
remove_if 가 지울 값을 뒤로 보내고 지울값 처음 위치를 반환한다. 
즉 erase를 함께 사용해야 한다.
특히 list의 remove, removeif맴버는 algorithm의 remove, remove_if와 좀 다른 동작을 합니다. 
list구조상 std::erase( std::remove(...) ) 계열쓰면 효율이 떨어 지게 때문에 그런 동작을 하는것을로 알고 있습니다. 

Ex.)
// edit 대상 instance 지우기
auto fnLambdaDel = [&]( LP_RCV_SCANSET_DYN pInfoScanset )->bool{ 
if( infoRange.timeBgn > pInfoScanset->timeScanData ||
infoRange.timeEnd < pInfoScanset->timeScanData) return false;
delete pInfoScanset;
return true;
};

// scanset 버퍼에서 지우기.. remove_if는 조건에 맞는 것을 뒤로 보낸다. 그리고 그 시작위치를 리턴한다.
auto itDelBgn = std::remove_if( m_vecScanset.begin(), m_vecScanset.end(), fnLambdaDel );
m_vecScanset.erase(itDelBgn, m_vecScanset.end());


template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred );

Remove elements from range

Applies pred to the elements in the range [first,last), and removes those for which it does not return false from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range.

The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}


Notice that in this example implementation the elements past the new end are not altered, but alternative implementations may change their values.

Parameters

first, last
Forward iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
pred
Unary predicate taking an element in the range as argument, and returning a value indicating the falsehood (with false, or a zero value) or truth (true, or non-zero) of some condition applied to it. This can either be a pointer to a function or an object whose class overloads operator().

Return value

A forward iterator pointing to the new end of the sequence, which now includes all the elements for which pred was false.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// remove_if example
#include <iostream>
#include <algorithm>
using namespace std;

bool IsOdd (int i) { return ((i%2)==1); }

int main () {
  int myints[] = {1,2,3,4,5,6,7,8,9};            // 1 2 3 4 5 6 7 8 9

  // bounds of range:
  int* pbegin = myints;                          // ^
  int* pend = myints+sizeof(myints)/sizeof(int); // ^                 ^

  pend = remove_if (pbegin, pend, IsOdd);        // 2 4 6 8 ? ? ? ? ?
                                                 // ^       ^
  cout << "range contains:";
  for (int* p=pbegin; p!=pend; ++p)
    cout << " " << *p;

  cout << endl;
 
  return 0;
}


Output:
range contains: 2 4 6 8

Complexity

Applies pred as many times as the number of elements in the range [first,last), and performs as many assignment operations as the number of elements not removed.

'삽질' 카테고리의 다른 글

[펌]테이블/ 테이블스페이스별 사용량 확인  (0) 2012.02.03
ImageSafer... Image Protection  (1) 2011.11.17
std::remove_if ....  (0) 2011.11.06
image 포맷 변환.  (0) 2011.11.04
ImageCodecInfo Class  (0) 2011.11.04
GetImageEncoders Function  (0) 2011.11.04
Posted by 촌돌애비
TAG ,

댓글을 달아 주세요