[C++] binary_search 정리

메모리 기반의 방대한 데이타가 있다고 가정을 할 때.. 이 데이터 목록 중 어떤 데이타가 존재하는지의 여부를 가장 빠르게 검색해주는 방식이 binary_search인데요. 이 STL 함수를 정리해 봅니다.

먼저 메모리 기반의 방대한 데이터를 준비해 봅니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };
}
#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 }; }
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };

}

데이타가 몇개 되지 않지만 방대하다고 칩시다. binary_search를 사용하기 위해서는 먼저 데이터가 정렬되어 있어야 합니다. 내림차순으로 정렬하는 코드를 추가하면.. 다음과 같습니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };
auto predicate = [](int a, int b) { return a > b; };
values.sort(predicate);
copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "});
cout << endl;
}
#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 }; auto predicate = [](int a, int b) { return a > b; }; values.sort(predicate); copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "}); cout << endl; }
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };
    auto predicate = [](int a, int b) { return a > b; };
    values.sort(predicate);

    copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "});
    cout << endl;
}

람다 함수로 내림차순 정렬을 위한 기준으로 삼았습니다. 그리고 잘 정렬되었는지 콘솔에 표시해 보았구요. 이제 binary_search 함수를 사용하기 위한 데이터 덩어리가 준비 되었고, 다음처럼 데이터 덩어리중 값 5가 존재하는지의 여부를 검색하는 코드는 다음과 같습니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };
auto predicate = [](int a, int b) { return a > b; };
values.sort(predicate);
copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "});
cout << endl;
int wanted{ 5 };
bool bSearched = binary_search(begin(values), end(values), wanted, predicate);
if (bSearched) {
cout << "Found !" << endl;
} else {
cout << "Not found." << endl;
}
}
#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 }; auto predicate = [](int a, int b) { return a > b; }; values.sort(predicate); copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "}); cout << endl; int wanted{ 5 }; bool bSearched = binary_search(begin(values), end(values), wanted, predicate); if (bSearched) { cout << "Found !" << endl; } else { cout << "Not found." << endl; } }
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    list<int> values{ 1, 2, 8, 7 ,6, 5, 4, 1, 9, 3, 5, 6, 7 };
    auto predicate = [](int a, int b) { return a > b; };
    values.sort(predicate);

    copy(values.begin(), values.end(), ostream_iterator<double>{ std::cout, " "});
    cout << endl;

    int wanted{ 5 };
    bool bSearched = binary_search(begin(values), end(values), wanted, predicate);
    if (bSearched) {
        cout << "Found !" << endl;
    } else {
        cout << "Not found." << endl;
    }
}

DuraMap-Xr을 이용한 좌표 변환 코드 샘플

DuraMap-Xr은 proj.4 기반의 문자열을 토대로 좌표계를 변환할 수 있는데요. 특히 좌표 변환시 10 파라메터에 해당하는 Molodensky-Badekas 모델을 지원합니다.

아래의 코드는 “대한민국 TM 중부원점(Bessel 타원체) – 10.405 보정”의 좌표계의 한 좌표인 (492385.95, 188096.47)를 “대한민국 TM 중부원점(GRS80 타원체)” 좌표계로 변환하는 코드 예입니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
XrMapLib.Coord Pt;
Pt.X = 492385.95;
Pt.Y = 188096.47;
XrMapLib.Projection proj = new XrMapLib.Projection();
// 대한민국 TM 중부원점(Bessel 타원체) - 10.405 보정
proj.SourceProj4String = "+proj=tmerc +lat_0=38N +lon_0=127.0028902777778E +ellps=bessel +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs";
// 대한민국 TM 중부원점(GRS80 타원체)
proj.TargetProj4String = "+proj=tmerc +lat_0=38N +lon_0=127E +ellps=GRS80 +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs";
// 10 파라메터 적용(towgs84 파라메터)
proj.Set10Parameters(-145.907, 505.034, 685.756, -1.162, 2.347, 1.592, 6.342, -3159521.31, 4068151.32, 3748113.85);
proj.Transform(Pt);
XrMapLib.Coord Pt; Pt.X = 492385.95; Pt.Y = 188096.47; XrMapLib.Projection proj = new XrMapLib.Projection(); // 대한민국 TM 중부원점(Bessel 타원체) - 10.405 보정 proj.SourceProj4String = "+proj=tmerc +lat_0=38N +lon_0=127.0028902777778E +ellps=bessel +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs"; // 대한민국 TM 중부원점(GRS80 타원체) proj.TargetProj4String = "+proj=tmerc +lat_0=38N +lon_0=127E +ellps=GRS80 +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs"; // 10 파라메터 적용(towgs84 파라메터) proj.Set10Parameters(-145.907, 505.034, 685.756, -1.162, 2.347, 1.592, 6.342, -3159521.31, 4068151.32, 3748113.85); proj.Transform(Pt);
XrMapLib.Coord Pt;

Pt.X = 492385.95;
Pt.Y = 188096.47;

XrMapLib.Projection proj = new XrMapLib.Projection();

// 대한민국 TM 중부원점(Bessel 타원체) - 10.405 보정
proj.SourceProj4String = "+proj=tmerc +lat_0=38N +lon_0=127.0028902777778E +ellps=bessel +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs";

// 대한민국 TM 중부원점(GRS80 타원체)
proj.TargetProj4String = "+proj=tmerc +lat_0=38N +lon_0=127E +ellps=GRS80 +x_0=200000 +y_0=600000 +k=1 +units=m +no_defs";

// 10 파라메터 적용(towgs84 파라메터)
proj.Set10Parameters(-145.907, 505.034, 685.756, -1.162, 2.347, 1.592, 6.342, -3159521.31, 4068151.32, 3748113.85);

proj.Transform(Pt);

코드를 보면 좌표계 지정을 위해서 사용한 proj.4 문자열에 towgs84에 해당하는 값을 14번째 코드처럼 별도의 매서드(Set10Parameters)로 지정한다는 것입니다. 3개 또는 7개의 파라메터 지정을 위해서는 각각 Set3Parameters, Set7Parameters 매서드를 사용합니다.

최종 좌표변환은 16번 코드의 Transform 매서드로 수행되는데요. 이 매서드는 변환할 입력 좌표 객체를 필요로 하고, 변환된 좌표는 다시 이 입력 좌표 객체에 저장되므로, 입력 좌표를 계속 유지하려면 복사본을 가지고 있어야 합니다.