mardi 3 août 2021

how to overload std::vector

namespace std {
  bool operator<(const vector<int>& v1, const vector<int>& v2)
  {
      if (v1[0] < v2[0] && v1[1] < v2[1])
          return false;
      else
          return true;
  }

}

int main() {
vector<vector<int>> a1{ {1,2},{3,4} };
sort(a1.begin(), a1.end());
for (auto num : a1)
    cout << num[0] << num[1] << endl;
return 0;

}

why when I remove the namespace std, and it doesn't work anymore?It's about scope of sort?And why it does not need template specialisation?(just like here said enter link description here)

Aucun commentaire:

Enregistrer un commentaire