기본 콘텐츠로 건너뛰기

std::list::remove_if(), std::list::sort()

link http://hyunity3d.tistory.com/244?category=527936
코드 돌려보고 c++11 형태로 문법오류 수정해서 동작 처리

#include <iostream>
#include <list>
#include <functional>   // std::unary_function

// 20 이상 30 미만이면 true
template <typename T> class Is_Over20_Under30 : public std::unary_function<T, bool> // argument type - T, result type - bool
{
public:
bool operator( ) (T& val)
{
return (val >= 20 && val < 30);
}
};

// 함수 객체 정의
template <typename T> struct COMPARE_ITEM
{
bool operator()(const T l, const T r) const
{
// 정렬 시에는 올림 차순으로된다. 내림 차순으로 하고 싶으면 < 에서 > 로 변경하면 된다.
return l < r;
}
};

int test8()
{
std::list< int > list1;

list1.push_back(10);
list1.push_back(20);
list1.push_back(25);
list1.push_back(30);
list1.push_back(34);

std::cout << std::endl << "remove_if  테스트 1 - 사용자가 정의한 조건에 의해 삭제" << std::endl;

// 20 이상 30 미만은 삭제한다.
list1.remove_if(Is_Over20_Under30< int >());

std::list< int >::iterator iterEnd = list1.end();
for (std::list< int >::iterator iterPos = list1.begin();
iterPos != iterEnd;
++iterPos)
{
std::cout << "list 1 : " << *iterPos << std::endl;
}

std::cout << std::endl << "sort()  테스트 2 - 작은순 소트 " << std::endl;

list1.sort(); // ascending

//std::list< int >::iterator iterEnd = list1.end();
for (std::list< int >::iterator iterPos = list1.begin();
iterPos != iterEnd;
++iterPos)
{
std::cout << "list 2 : " << *iterPos << std::endl;
}

std::cout << std::endl << "sort(std::greater< int >())  테스트 2 - 큰순 소트 " << std::endl;

list1.sort(std::greater< int >()); // descending

//std::list< int >::iterator iterEnd = list1.end();
for (std::list< int >::iterator iterPos = list1.begin();
iterPos != iterEnd;
++iterPos)
{
std::cout << "list 2 : " << *iterPos << std::endl;
}

std::cout << std::endl << "sort(COMPARE_ITEM<int>())  테스트 2 - 사용자가 정의한 소트 " << std::endl;

list1.sort(COMPARE_ITEM< int >()); // 작은 순

//std::list< int >::iterator iterEnd = list1.end();
for (std::list< int >::iterator iterPos = list1.begin();
iterPos != iterEnd;
++iterPos)
{
std::cout << "list 2 : " << *iterPos << std::endl;
}

return 0;
}

}int _tmain(int argc, _TCHAR* argv[])
{
return test8();
}

댓글

이 블로그의 인기 게시물

[c#.net] WPF 사용 중 xaml관련 실행시 오류 와 대처 방법

wpf 사용 중 xaml관련 실행시 오류 가 발생해서 자그마치 2일을 소비 했다..--;; 오류 내용은 아래와 같다. ================================== 'System.Windows.Markup.XamlParseException' 형식의 첫째 예외가 PresentationFrameWork.dll에서 발생했습니다. 추가 정보: '지정된 바인딩 제약 조건과 일치하는 'PapaZone.MainWindow' 형식에 대한 생성자 호출에서 예외가 throw 되었습니다.' 줄 번호 '21' 및 줄 위치 '9' 이 예외에 대한 처리기가 있으면 프로그램을 안전하게 계속할 수 있습니다. [] 이 예외 형식이 throw되면 중단 ================================== 위 Xaml 오류 메세지만으로는 내용 추적 불가하여, 도대체 뭔가 했다. 검색해도 뚜렷한 답변은 없었지만, 여러가지 의견을 종합해 보면 아래와 같다. 1. xmlns 관련 파일 과 관련된 파일 로딩 실패 이거나, 2. 신규로 추가된 namespace 에 속한 태그 사용시 오류로 판단되어 태그 하나씩 삽입하며 테스트 해봐야 한다고 함. 해결한 지금에 원인을 보면, xaml 에서 clr-namespace 로 추가된 항목들이 로딩에 실패해서 발생한 것이며, 추가된 항목은 있었으나, 이것과 연결된 파일이 없어서 로딩되지 않는 것이 문제였다. 오늘도 즐거운 하루~~

golang .protobuf go_package 오류 나는 문제.

1.golang용 protocol buffer 관련 모듈은 다운로드 받았음 go get -d -u google.golang.org/protobuf go install github.com/golang/protobuf/protoc-gen-go 2. pitaya 샘플 .proto 파일 생성을 시도 했으나, 아래 오류가 발생하면서 빌드 실패함 D:\pitaya>protoc --proto_path=examples\demo\worker\protos --go_out=plugins=grpc:. examples\demo\worker\protos\*.proto protoc-gen-go: unable to determine Go import path for "arg.proto" Please specify either:         • a "go_package" option in the .proto source file, or         • a "M" argument on the command line. See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information. --go_out: protoc-gen-go: Plugin failed with status code 1. D:\pitaya> stackoverflow 등 참고해도 비슷하지만 다른 내용들 뿐. .proto 파일내에 option go_package = "../protos_gen"; 내용 추가하여 빌드하니 잘됨. 3.기존 소스 -------------------------------- syntax = "proto3"; package protos; message Arg {   string msg = 1; } 4.신규 소스 -------------------------------- syntax ...