📄 sortedrange_algo_test.cpp
字号:
/**********************************************************************************
* *
* Copyright (c) by Royal. All rights reserved. *
* *
* Permission to use, copy, modify, and distribute this software for any purpose *
* is hereby granted without fee, provided that this copyright and permissions *
* notice appear in all copies and derivatives, and that no charge may be made *
* for the software and its documentation except to cover cost of distribution. *
* *
* This software is provided "as is" without express or implied warranty. *
* *
**********************************************************************************/
/*
* Description:
*
* Examples of sortedrange algorithm.
*
* History:
*
* Initial version created by Xyb, May, 2004.
*
* Notes:
*
* This code has been written to conform to standard C++ and STL. It has been
* compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/
#include "algostuff.h"
using namespace std;
int main()
{
/*
*调用者须自己确保使用以下算法时区间已经排序!!!
*/
//binary_search
cout << endl << "binary_search: " << endl;
vector<int> coll1;
Insert_Elements(coll1, 1, 9);
Print_Elements(coll1, "coll1: ");
if (binary_search(coll1.begin(), coll1.end(), 8))
{
cout << "8 is in coll1. " << endl;
}
else
{
cout << "8 is not in coll1." << endl;
}
//includes
cout << endl << "includes: " << endl;
vector<int> coll2;
Insert_Elements(coll2, 5, 8);
Print_Elements(coll2, "coll2; ");
if (includes(coll1.begin(), coll1.end(), coll2.begin(), coll2.end()))
{
cout << "all coll2's elements are in coll1." << endl;
}
else
{
cout << "not all the coll2's elements are in coll1." << endl;
}
//lower_bound & upper_bound
cout << endl << "lower_bound & upper_bound: " << endl;
vector<int>::iterator pos1, pos2;
Insert_Elements(coll1, 1, 9);
sort(coll1.begin(), coll1.end());
Print_Elements(coll1, "coll1: ");
pos1 = lower_bound(coll1.begin(), coll1.end(), 3);
pos2 = upper_bound(coll1.begin(), coll1.end(), 3);
cout << "3 could insert into "
<< distance(coll1.begin(), pos1) + 1
<< " up to "
<< distance(coll1.begin(), pos2) + 1
<< endl;
//equal_range
cout << endl << "equal_range:" << endl;
pair<vector<int>::iterator, vector<int>::iterator> range;
range = equal_range(coll1.begin(), coll1.end(), 3);
cout << "3 could insert into "
<< distance(coll1.begin(), range.first) + 1
<< " up to "
<< distance(coll1.begin(), range.second) + 1
<< endl;
//merge
cout << endl << "merge:" << endl;
vector<int> coll3;
Print_Elements(coll1, "coll1: ");
Print_Elements(coll2, "coll2: ");
merge(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), back_inserter(coll3));
Print_Elements(coll3, "coll3: ");
//set_union
cout << endl << "set_union:" << endl;
coll3.clear();
Print_Elements(coll1, "coll1: ");
Print_Elements(coll2, "coll2: ");
set_union(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), back_inserter(coll3));
Print_Elements(coll3, "coll3: ");
//set_intersection
cout << endl << "set_intersection" << endl;
coll3.clear();
Print_Elements(coll1, "coll1: ");
Print_Elements(coll2, "coll2: ");
set_intersection(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), back_inserter(coll3));
Print_Elements(coll3, "coll3: ");
//set_difference
cout << endl << "set_difference: " << endl;
coll3.clear();
Print_Elements(coll1, "coll1: ");
Print_Elements(coll2, "coll2: ");
set_difference(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), back_inserter(coll3));
Print_Elements(coll3, "coll3: ");
//set_symmetric_difference
cout << endl << "set_symmetric_difference :" << endl;
coll3.clear();
Print_Elements(coll1, "coll1: ");
Print_Elements(coll2, "coll2: ");
set_symmetric_difference(coll1.begin(), coll1.end(), coll2.begin(), coll2.end(), back_inserter(coll3));
Print_Elements(coll3, "coll3: ");
//inplace_merge
cout << endl << "inplace_merge:" << endl;
coll1.clear();
Insert_Elements(coll1, 1, 9);
Insert_Elements(coll1, 1, 9);
Print_Elements(coll1, "coll1: ");
inplace_merge(coll1.begin(), coll1.begin() + 9, coll1.end());
Print_Elements(coll1, "coll1: ");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -