Applications of is_permutation()

What are some practical applications of is_permutation()?

std::ranges::is_permutation() is a versatile function that can be used in various scenarios where the order of elements does not matter, but their presence does. Here are some practical applications:

Data Validation

Ensure that two datasets contain the same elements, regardless of order. This is useful in test cases where the result's order might differ due to algorithmic variations but should still contain the same data.

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> expected{1, 2, 3};
  std::vector<int> result{3, 1, 2};

  if (std::ranges::is_permutation(expected, result)) {
    std::cout << "Test passed";
  } else {
    std::cout << "Test failed";
  }
}
Test passed

Anagram Detection

Check if two strings are anagrams of each other. Anagrams are words or phrases that contain the same letters in different orders.

#include <algorithm>
#include <iostream>
#include <string>

int main() {
  std::string word1 = "listen";
  std::string word2 = "silent";

  if (std::ranges::is_permutation(word1, word2)) {
    std::cout << "The words are anagrams";
  } else {
    std::cout << "The words are not anagrams";
  }
}
The words are anagrams

Schedule Matching

Verify if two event schedules contain the same events, irrespective of their order. This can be useful in applications where the order of events may vary but the events themselves should match.

#include <algorithm>
#include <iostream>
#include <vector>

class Event {
 public:
  Event(std::string name, int time)
    : name{name}, time{time} {}
  std::string getName() const { return name; }
  int getTime() const { return time; }

 private:
  std::string name;
  int time;
};

int main() {
  std::vector<Event> schedule1{
    {"Meeting", 9},
    {"Lunch", 12},
    {"Presentation", 15}
  };

  std::vector<Event> schedule2{
    {"Presentation", 15},
    {"Meeting", 9},
    {"Lunch", 12}
  };

  auto eventEqual = [](
    const Event& e1, const Event& e2) {
    return e1.getName() == e2.getName() &&
      e1.getTime() == e2.getTime();
  };

  if (std::ranges::is_permutation(
    schedule1, schedule2, eventEqual)) {
    std::cout << "Schedules match";
  } else {
    std::cout << "Schedules do not match";
  }
}
Schedules match

Inventory Management

Compare inventory lists from different stores to ensure they contain the same items, regardless of order.

#include <algorithm>
#include <iostream>
#include <vector>

class Item {
 public:
  Item(std::string name, int quantity)
    : name{name}, quantity{quantity} {}
  std::string getName() const { return name; }
  int getQuantity() const { return quantity; }

 private:
  std::string name;
  int quantity;
};

int main() {
  std::vector<Item> inventory1{
    {"Apples", 50},
    {"Bananas", 30},
    {"Oranges", 40}
  };

  std::vector<Item> inventory2{
    {"Oranges", 40},
    {"Apples", 50},
    {"Bananas", 30}
  };

  auto itemEqual = [](
    const Item& i1, const Item& i2) {
    return i1.getName() == i2.getName()
      && i1.getQuantity() == i2.getQuantity();
  };

  if (std::ranges::is_permutation(
    inventory1, inventory2, itemEqual)) {
    std::cout << "Inventories match";
  } else {
    std::cout << "Inventories do not match";
  }
}
Inventories match

In summary, std::ranges::is_permutation() is useful in scenarios where the presence of elements matters more than their order. It can be applied in various domains such as data validation, anagram detection, schedule matching, and inventory management.

Comparison Algorithms

An introduction to the eight main comparison algorithms in the C++ Standard Library

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

std::equal() vs std::is_permutation()
What is the difference between equal() and is_permutation()?
Custom Comparison for std::equal()
How do I customize the comparison behavior for equal()?
Handling Mismatch Results
How do I handle past-the-end iterators returned by mismatch()?
Equal vs Lexicographical Compare
When should I use lexicographical_compare() over equal()?
Comparing Different Length Ranges
How do I compare two ranges of different lengths using mismatch()?
Understanding Identity Permutations
What are identity permutations and why are they important?
Comparing Floating Point Numbers
How do I compare ranges that contain floating-point numbers?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant