本文整理汇总了C++中Comparator类的典型用法代码示例。如果您正苦于以下问题:C++ Comparator类的具体用法?C++ Comparator怎么用?C++ Comparator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Comparator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: scan
int32 Vector::scan(char* key, uint32 sz, uint32* index, Comparator* c)
{
Comparator* cUse = NULL;
int32 ret = 0;
if (!c && !m_comp)
return FAILURE;
if (!c)
cUse = m_comp;
else
cUse = c;
for (uint32 j = 0; j < getCount(); j++) {
VectorSlot* vs = (VectorSlot*) m_pvDB[j];
if (vs) {
ret = cUse->compare(vs->dbData.toString(),
vs->dbData.getCount(), key, sz);
if (ret == SUCCESS) {
if (index)
*index = j;
return SUCCESS;
}
}
}
return FAILURE;
}
示例2: partition
int partition(vector<string> &nuts, vector<string> &bolts, int s, int e, Comparator compare) {
for(int i = s; i <= e; i++) {
if(compare.cmp(nuts[s], bolts[i]) == 0) {
swap(bolts[s], bolts[i]);
break;
}
}
int p = s+1, i = s+1;
while(i <= e) {
if(compare.cmp(nuts[s], bolts[i]) == 1) {
swap(bolts[p], bolts[i]);
p++;
}
i++;
}
swap(bolts[s], bolts[--p]);
swap(nuts[s], nuts[p]);
i = s;
int j = p+1;
while(i <= p-1 && j <= e) {
if(compare.cmp(nuts[i], bolts[p]) == 1) {
swap(nuts[i], nuts[j]);
j++;
} else {
i++;
}
}
return p;
}
示例3: main
int main (void) {
//int x = 123, y = 456;
double x = 1.23, y = 4.56;
//Comparator<int> comp (x, y);
Comparator<double> comp (x, y);
cout << comp.max () << ' ' << comp.min ()
<< endl;
cout << ::max (x, y) << ' ' << ::min (x, y)
<< endl;
return 0;
}
示例4: if
/**
* Compares two solutions.
* @param solution1 Object representing the first <code>Solution</code>.
* @param solution2 Object representing the second <code>Solution</code>.
* @return -1, or 0, or 1, or 2 if solution1 is dominates solution2, solution1
* and solution2 are equals, or solution1 is greater than solution2,
* respectively.
*/
int EqualSolutions::compare(Solution * solution1, Solution * solution2) {
if (solution1==NULL)
return 1;
else if (solution2 == NULL)
return -1;
int dominate1 ; // dominate1 indicates if some objective of solution1
// dominates the same objective in solution2. dominate2
int dominate2 ; // is the complementary of dominate1.
dominate1 = 0 ;
dominate2 = 0 ;
int flag;
double value1, value2;
for (int i = 0; i < solution1->getNumberOfObjectives(); i++) {
Comparator * c = new ObjectiveComparator(i);
flag = c->compare(solution1,solution2);
delete c;
value1 = solution1->getObjective(i);
value2 = solution2->getObjective(i);
if (value1 < value2) {
flag = -1;
} else if (value1 > value2) {
flag = 1;
} else {
flag = 0;
}
if (flag == -1) {
dominate1 = 1;
}
if (flag == 1) {
dominate2 = 1;
}
}
if (dominate1== 0 && dominate2 ==0) {
return 0; //No one dominate the other
}
if (dominate1 == 1) {
return -1; // solution1 dominate
} else if (dominate2 == 1) {
return 1; // solution2 dominate
}
return 2;
} // compare
示例5: main
int main(){
Comparator comp(3,4);
Comparator comp1(25.1f,45.6f);
Comparator comp2('v','c');
Comparator comp3("happy","sad");
cout<< "\nUse of copy constructor" << endl;
cout << "-----------------------" << endl;
Comparator compCopy = comp;
compCopy.check();
return 0;
}
示例6: sort
void sort(vector<string> &nuts, vector<string> &bolts, int left, int right, Comparator compare) {
if (left >= right) return;
fprintf(stderr, "%d %d ", left, right);
int ni, nj, bi, bj, m;
ni = bi = left; nj = bj = right;
m = (left + right) >> 1;
// recover bolts
int bp = 0;
while (bi < bj) {
while (bi < bj && compare.cmp(nuts[m], bolts[bi]) < 0) bi ++;
while (bi < bj && compare.cmp(nuts[m], bolts[bj]) > 0) bj --;
if (compare.cmp(nuts[m], bolts[bi]) == 0) bp = bj; else
if (compare.cmp(nuts[m], bolts[bj]) == 0) bp = bi;
if (bi < bj) {
swap(bolts, bi, bj);
bi ++; bj --;
}
// print(bolts);
}
// printf("%d %d\n", bi, bp);
while (compare.cmp(nuts[m], bolts[bi]) < 0) bi ++;
fprintf(stderr, "%d %d ", bi, bp);
if (bp < bi) { swap(bolts, bp, bi - 1); bp = bi - 1; } else
if (bp > bi) { swap(bolts, bp, bi); bp = bi; }
// print(bolts);
// recover nuts
swap(nuts, m, bp);
m = bp;
int np = 0;
while (ni < m) {
while (ni < m && compare.cmp(nuts[ni], bolts[m]) > 0) ni ++;
while (ni < m && compare.cmp(nuts[nj], bolts[m]) < 0) nj --;
if (ni < m) {
swap(nuts, ni, nj);
ni ++; nj --;
}
}
// print(nuts);
fprintf(stderr, "%d|", m);
sort(nuts, bolts, left, m - 1, compare);
sort(nuts, bolts, m + 1, right, compare);
}
示例7: m_x
/*
template<typename T>
Comparator<T>::Comparator (T x, T y) :
m_x (x), m_y (y) {}
template<typename T>
T Comparator<T>::max (void) const {
return m_x < m_y ? m_y : m_x;
}
template<typename T>
T Comparator<T>::min (void) const {
return m_x < m_y ? m_x : m_y;
}
*/
int main (void) {
Comparator<int> ci (123, 456);
cout << ci.max() << ' ' << ci.min() << endl;
Comparator<double> cd (1.23, 4.56);
cout << cd.max() << ' ' << cd.min() << endl;
Comparator<string> cs ("hello", "world");
cout << cs.max() << ' ' << cs.min() << endl;
Comparator<char const*> cp("hello","world");
cout << cp.max() << ' ' << cp.min() << endl;
return 0;
}
示例8: e
void ContainerTest::outputExpectedSmaller() {
std::stringstream out;
std::vector<int> a{1, 2, 3, 4};
std::vector<int> b{1, 2, 3};
{
Error e(&out);
Comparator<Compare::Container<std::vector<int>>> compare;
CORRADE_VERIFY(!compare(a, b));
compare.printErrorMessage(e, "a", "b");
}
CORRADE_COMPARE(out.str(), "Containers a and b have different size, actual 4 but 3 expected. Actual has 4 on position 3.\n");
}
示例9: sort_template
void sort_template(xptr_sequence *xs, int off, int len)
{
Comparator comp;
// Insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && comp.on_less(xs, j,j-1)<0; j--)
xs->swap(j, j-1);
return;
}
// Choose a partition element, v
int m = off + (len >> 1); // Small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // Big arrays, pseudomedian of 9
int s = len/8;
l = comp.med3(xs, l, l+s, l+2*s);
m = comp.med3(xs, m-s, m, m+s);
n = comp.med3(xs, n-2*s, n-s, n);
}
m = comp.med3(xs, l, m, n); // Mid-size, med of 3
}
xptr v = xs->get(m);
// Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && comp.on_less_lt(xs, b, v)<=0) {
if (xs->get(b) == v)
xs->swap(a++, b);
b++;
}
while (c >= b && comp.on_less_rt(xs,v,c)<=0) {
if (xs->get(c) == v)
xs->swap(c, d--);
c--;
}
if (b > c)
break;
xs->swap(b++, c--);
}
// Swap partition elements back to middle
int s, n = off + len;
s = s_min(a-off, b-a ); xs->vecswap( off, b-s, s);
s = s_min(d-c, n-d-1); xs->vecswap( b, n-s, s);
// Recursively sort non-partition-elements
if ((s = b-a) > 1)
sort_template<Comparator>(xs, off, s);
if ((s = d-c) > 1)
sort_template<Comparator>(xs, n-s, s);
}
示例10: quicksort
void quicksort(vector<string>& nuts, vector<string>& bolts, int begin, int end, Comparator cmp) {
if (begin >= end) return;
int pivot_pos;
int left = 0;
for (int i = begin; i <= end; ++i) {
int res = cmp.cmp(nuts[begin], bolts[i]);
if (res == 0) {
// find the corresponding place for bolt
pivot_pos = i;
} else if (res == 1) {
// find number of smaller bolts
++left;
}
}
// put pivot into position
swap(nuts, begin, begin+left);
swap(bolts, pivot_pos, begin+left);
pivot_pos = begin + left;
// do mutual quicksort
int i = begin, j = end;
while (i < pivot_pos && pivot_pos < j) {
while (i < pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[i]) == 1) ++i;
while (j > pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[j]) == -1) --j;
if (i < j) {
swap(bolts, i, j);
++i;
--j;
}
}
i = begin, j = end;
while (i < pivot_pos && pivot_pos < j) {
while (i < pivot_pos && cmp.cmp(nuts[i], bolts[pivot_pos]) == -1) ++i;
while (j > pivot_pos && cmp.cmp(nuts[j], bolts[pivot_pos]) == 1) --j;
if (i < j) {
swap(nuts, i, j);
++i;
--j;
}
}
quicksort(nuts, bolts, begin, pivot_pos-1, cmp);
quicksort(nuts, bolts, pivot_pos+1, end, cmp);
}
示例11: partition
// All the smaller elements should be in the left side of the pivot,
// and all the bigger elements should in the right side of the pivot.
int partition(vector<string>& arr,
int left, int right, const string& pivot,
Comparator& compare) {
for (int i = left; i < right; ) {
if (compare.cmp(arr[i], pivot) == SMALLER || // Smaller.
(compare.cmp(arr[i], pivot) == REVERSE &&
compare.cmp(pivot, arr[i]) == BIGGER)) {
swap(arr[left++], arr[i++]);
} else if (compare.cmp(arr[i], pivot) == BIGGER || // Bigger.
(compare.cmp(arr[i], pivot) == REVERSE &&
compare.cmp(pivot, arr[i]) == SMALLER)) {
++i;
} else { // Equal.
swap(arr[i], arr[right]);
}
}
// Put the pivot to the partition index.
swap(arr[left], arr[right]);
// Return the partition index of an array.
return left;
}
示例12: partition
int partition(vector<string> &str, string pivot, Comparator compare, int l, int u){
int mid = l; // middle
for (int i = l + 1; i <= u; i++) {
if (compare.cmp(str[i], pivot) == -1 ||
compare.cmp(pivot, str[i]) == 1) {
// str[i] smaller than pivot
mid++;
swap(str, i, mid);
} else if (compare.cmp(str[i], pivot) == 0 ||
compare.cmp(pivot, str[i]) == 0) {
// swap nuts[l]/bolts[l] with pivot
// l位置放pivot
swap(str, i, l);
i--;
}
}
// move pivot to proper index
swap(str, mid, l);
return mid;
}
示例13: main
int main(int ac, char** av)
{
// checking args
if (ac != 3)
{
std::cerr << "Usage: " << av[0] << " <file1> <file2>" << std::endl;
std::cerr << "<file1>: list of hashes" << std::endl;
std::cerr << "<file2>: list of words" << std::endl;
return -1;
}
// getting datas
Parser parser;
std::string file1 = av[1], file2 = av[2];
std::vector<std::string>& hashes = parser.GetLines(file1);
std::vector<std::string>& words = parser.GetLines(file2);
// get matches with time
Comparator comparator;
clock_t begin, end;
begin = clock();
comparator.ShowMatches(hashes, words);
end = clock();
double elapsed = double(end - begin) / CLOCKS_PER_SEC;
std::cout.precision(5);
std::cout << "time elapsed: " << elapsed << " second(s)" << std::endl;
// cleaning
hashes.clear();
delete &hashes;
words.clear();
delete &words;
return 0;
}
示例14: sortNutsAndBoltsN2
/**
* @param nuts: a vector of integers
* @param bolts: a vector of integers
* @param compare: a instance of Comparator
* @return: nothing
*/
void sortNutsAndBoltsN2(vector<string> &nuts, vector<string> &bolts, Comparator compare) {
int n = nuts.size();
int m = bolts.size();
for (int i = 0; i < n - 1; i ++) {
int j = i;
while (j < m && compare.cmp(nuts[i], bolts[j])) j ++;
string temp = bolts[j];
bolts[j] = bolts[i];
bolts[i] = temp;
}
for (int i = 0; i < n; i ++) {
printf("%s ", bolts[i].c_str());
}
}
示例15: binarySearch
long binarySearch(const S searched,
const std::size_t searchLength,
const T& searchedFor,
const Comparator<T>& comp)
{
std::size_t low(0);
std::size_t high(searchedLength);
while( low < high ) {
std::size_t mid(low + ((high - low) >> 1));
switch(comp.compare(searched[mid], searcedFor))
{
case ORDER_INCREASING:
low = mid + 1;
break;
case ORDER_DECREASING:
high = mid;
break;
case ORDER_SAME:
return static_cast<long>(mid);
}
}
return -1 - static_cast<long>(low);
}