Updated:

2 minute read

개요

  • sort
  • stable_sort
  • partial_sort
    • partial_sort(a, b, c);
    • a부터 c까지의 데이터 중에 b 까지만 정렬
  • remove, remove_if
  • transform


예제

  • 코드
    • #include <algorithm>
      #include <iostream>
      #include <vector>
             
      using namespace std;
             
      class Test {
          private:
              int i;
              char c;
             
          public:
              Test(int i, char c) : i(i), c(c) {}
              ~Test() = default;
             
              int GetI() const { return this->i; }
              void SetI(const int& i) { this->i = i; }
             
              bool operator<(const Test& t) const { return this->i < t.i; }
              bool operator==(const Test& t) const { return this->i == t.i; }
             
              friend ostream& operator<<(std::ostream& o, const Test& t);
      };
      ostream& operator<<(std::ostream& o, const Test& t) {
          o << t.i << ", " << t.c;
          return o;
      }
             
      void make_data(vector<Test>& v, const int& count = 100) {
          v.clear();
             
          for (int i = count; i > 0; --i) {
              v.push_back(Test(i % 10, 'a' + i % 26));
          }
      }
             
      int main() {
          vector<Test> v;
             
          make_data(v);
          sort(v.begin(), v.end());
          for (int i = 0; i < 5; ++i) {
              cout << "v-1 : " << v.at(i) << endl;
          }
             
          make_data(v);
          stable_sort(v.begin(), v.end());
          for (int i = 0; i < 5; ++i) {
              cout << "v-2 : " << v.at(i) << endl;
          }
             
          make_data(v, 5);
          partial_sort(v.begin(), v.begin() + 2, v.end());
          for (const auto& iter : v) {
              cout << "v-3 : " << iter << endl;
          }
             
          make_data(v, 5);
          v.erase(remove(v.begin(), v.end(), Test(1, 'a')), v.end());
          for (const auto& iter : v) {
              cout << "v-4 : " << iter << endl;
          }
          v.erase(remove_if(v.begin(), v.end(), [](const Test& t) { return t.GetI() > 3; }),
                  v.end());
          for (const auto& iter : v) {
              cout << "v-5 : " << iter << endl;
          }
             
          make_data(v, 5);
          transform(v.begin(), v.end(), v.begin(), [](Test& t) {
              t.SetI(t.GetI() + 1);
              return t;
          });
          for (const auto& iter : v) {
              cout << "v-6 : " << iter << endl;
          }
             
          return 0;
      }
      
  • 실행 결과
    • v-1 : 0, w
      v-1 : 0, k
      v-1 : 0, u
      v-1 : 0, c
      v-1 : 0, e
      v-2 : 0, w
      v-2 : 0, m
      v-2 : 0, c
      v-2 : 0, s
      v-2 : 0, i
      v-3 : 1, b
      v-3 : 2, c
      v-3 : 5, f
      v-3 : 4, e
      v-3 : 3, d
      v-4 : 5, f
      v-4 : 4, e
      v-4 : 3, d
      v-4 : 2, c
      v-5 : 3, d
      v-5 : 2, c
      v-6 : 6, f
      v-6 : 5, e
      v-6 : 4, d
      v-6 : 3, c
      v-6 : 2, b