cpp algorithm 1
include <algorithm>
+ test ; C++11
all_of() ; 모두 성공인지 테스트
any_of() ; 하나라도 성공인지 테스트
none_of() ; 모두 실패인지 테스트.
ex)
std::array<int, 8> foo = {3,5,7,11,13,17,19,23} ;
if ( std::all_of( foo.begin(), foo.end(), [](int i){ return i%2 ;} ) )
std::cout << " all the elements are odd numbers.\n" ;
std::array<int, 7> foo={0, 1, -1, 3, -3, 5, -5} ;
if ( std::any_of(foo.begin(), foo.end(), [](int i) { return i<0 ; }) )
std::cout << "there are negative elements.\n" ;
if ( std::none_of(foo.begin(), foo.end() [](int i) { return i<0 ; } ) )
std::cout << "there are no negative elements.\n" ;
+for_each ; 모든 엘리먼트에 함수 적용.
void myfunction(int i) { std::cout << i << ' '; }
struct myclass {
void operator() (int i) {std::cout << i << ' ';}
} myobj ;
std::vector<int> myvec ;
myvec.push_back(1) ;
myvec.push_back(2) ;
for_each( myvec.begin(), myvec.end(), myfunction) ;
for_each( myvec.begin(), myvec.end(), myobj) ;
+find ; 엘리먼트 값 찾기
찾아서 이터레이터 리턴. 없으면 last.return
ex) 3의 위치 찾기.
int myints[]={1,2,3,4} ;
int *p ;
p = std::find (myints, myints+4, 3) ;
if ( p!=myints+4 ) std::cout << "found : " << *p << '\n' ;
else std::cout << "not found\n" ;
std::vector<int> myvec(myints, myints+4) ;
std::vector<int>::iterator it ;
it = find( myvec.begin(), myvec.end(), 3) ;
if ( it!=myvec.end() ) std::cout << "found:" << *it << '\n' ;
else std::cout << "not found\n" ;
+find_if ; 조건을 만족하는 처음 위치 찾기 (엘리먼트별)
bool isOdd(int i) { return ( (i%2)==1 ) ; }
ex) 처음 나온 홀수 찾기
it = std::find_if( myvec.begin(), myvec.end(), isOdd) ;
if ( it!=myvec.end() ) std::cout << "the first odd value is " << *it << '\n';
else std::cout << "not found\n" ;
조건을 반대로 쓰려면 find_if_not(C++11) 사용.
+find_end ; 조건을 만족하는 마지막 위치 찾기 ( 그룹별)
ex) 1,2,3,4,5,1,2,3,4,5 에서 1,2,3 시퀀스를 만족하는 마지막 발견 위치 찾기
int myints[]={1,2,3,4,5,1,2,3,4,5} ;
std::vector<int> myvec (myints, myints+10) ;
int needle[] = {1,2,3} ;
it = std::find_end( myvec.begin(), myvec.end(), needle, needle+3 ) ;
=> it는 2번째 나온 1을 가리킴.
index는 it - myvec.begin() ;
it = std::find_end( myvec.begin(), myvec.end(), needle, needle+3, myfunction ) ;
bool myfunction(int i, int j) { return i==j ; }
+find_first_of ; 조건을 만족하는 처음 위치 찾기 ( 그룹별)
ex) 1,2,3이 처음 나온 곳 찾기.
it = find_first_of( myvec.begin(), myvec.end(), needle, needle+3) ;
it = find_first_of( myvec.begin(), myvec.end(), needle, needle+3, myfunction) ;
bool myfunction(char c1, char c2) { return (std::tolower(c1)==std::tolower(c2)) ; } // case-insensitive compare.
+adjacent_find ; 처음으로 조건을 만족하는 위치 찾기. (2개씩 그룹으로 자체 비교)
ex) 처음으로 연속된 값이 2개 나오는 곳 찾기
int myints[]={5,20,5,30,30,20,10,10,20} ;
std::vector<int> myvec(myints, myints+9) ;
it = std::adjacent_find( myvec.begin(), myvec.end()) ;
// *it 의 값은 30
it = std::adjacent_find( ", ", myfunction) ; // 조건을 별도로 설정 가능.
bool myfunction (int i, int j) { return i==i; }
+count ; 특정값의 엘리먼트 개수 세기
int mycount = std::count(myints, myints+8, 10) ; // 원소값이 10인 것 개수 세기.
mycount = std::count(myvec.begin(), myvec.end(), 20) ; // 원소값이 20인 것 개수 세기.
+count_if ; 조건에 맞는 엘리먼트 수 세기
// 홀수 개수 세기
mycount = std::count_if(myvec.begin(), myvec.end(), isOdd) ;
+mismatch ; 두 컨테이너의 값을 비교하여 처음으로 틀린 곳 찾기
std::pair<std::vector<int>::iterator, int*> mypair ;
mypair = std::mismatch( myvec.begin(), myvec.end(), myints ) ;
// *mypair.first => 처음 틀린 myvec의 원소값., *mypair.second => 처음 틀린 myints의 원소값.
mypair.first++; mypair.second++ ; // 찾은 곳 다음 위치로 이동.
mypair = std::mismatch( mypair.first, myvec.end(), mypair.second, mypredict ) ;
// 찾은 곳 다음부터 비교함수를 통해 언매치된 곳을 또 찾음.
+equal ; 두 컨테이너의 값이 일치하는지 테스트. (범위 지정 가능)
std::equal( myvec.begin(), myvec.end(), myints) ==> true/false 리턴, myints 대신 myvec2.begin() 이렇게도 가능.
std::equal( myvec.begin(), myvec.end(), myints, mypredict) // 비교함수 지정.
+is_permutation ; 순서가 달라도 범위내에 모든 엘리먼트들이 매치하는지 검사 (C++11)
std::array<int, 5> foo={1,2,3,4,5} ;
std::array<int, 5> bar={3,1,4,5,2} ;
if ( std::is_permutation(foo.begin(), foo.end(), bar.begin()) )
std::cout << "foo and bar contain the same elements.\n" ;
+search ; 일부분을 찾음. 스트링으로 치면 find로 일부분 찾기.
it = std::search(myvec.begin(), myvec.end(), needle1, needle1+4) ;
// it는 needle1이 myvec에 있는 첫번째 지점 시작위치를 가리킴. 없으면 myvec.end() 위치를 가리킴. 인덱스는 it-myvec.begin()
ex) 1,2,3,4,5 에서 2,3을 찾기
+search_n ; n개의 같은 값을 찾음. (시작,끝, 반복횟수, 값)
it = std::search_n(myvec.begin(), myvec.end(), 2, 30) ; // 30,30 이렇게 두 개 가 있는 곳을 찾음.
ex), 10, 20, 30,30,40,50,50 에서 30,03찾기.