का उपयोग क्यों std नहीं है :: तरह this codeक्यों std नहीं है :: तरह मेरी ऑपरेटर <कार्यान्वयन
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
bool operator<(
const tuple<int, int>& t1,
const tuple<int, int>& t2
) {
return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
}
int main() {
vector<tuple<int, int>> v;
for (int i = 0; i < 10; ++i) {
v.push_back(make_tuple(0, i));
}
cout << "before sort: ";
for (auto& x : v) { cout << get<1>(x) << ", "; }
cout << endl;
auto v2 = v;
sort(v2.begin(), v2.end());
cout << "after sort(begin, end): ";
for (auto& x : v2) { cout << get<1>(x) << ", "; }
cout << endl;
sort(v.begin(), v.end(), [](auto& t1, auto& t2) {
return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
});
cout << "after sort(begin, end, comparator): ";
for (auto& x : v) { cout << get<1>(x) << ", "; }
cout << endl;
return 0;
}
में मेरी operator<
कार्यान्वयन का उपयोग उत्पादन होता है:
before sort: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end, comparator): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
उत्पादन मैं उम्मीद है:
before sort: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
after sort(begin, end): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
after sort(begin, end, comparator): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
यदि आप इसे देखते हैं http://ideone.com/0h1rYa यह आपके कस्टम 'ऑपरेटर <' को सीधे मानक नेमस्पेस में ले जाने के बाद काम करता है (_ सुझाव नहीं देता कि एक अच्छा विचार_)। तो जब 'std :: tuple' to 'ऑपरेटर <'से मिलान करते समय यह मानक परिभाषित एक (' नेमस्पेस std' में) पहले (और/या बेहतर मिलान के रूप में) पाता है। –
Chad