Let's say you have a table
Customer | Product
Dave | Sneakers
Martin | Tooth Brush
Andrew | Shirt
Dave | Tooth Brush
In SQL, you can do
SELECT Customer, Product, DENSE_RANK() OVER (ORDER BY Customer) AS customer_id, DENSE_RANK() OVER (ORDER BY Product) AS product_id
Customer | Product | customer_id | product_id
Dave | Sneakers | 2 | 2
Martin | Tooth Brush | 3 | 3
Andrew | Shirt | 1 | 1
Dave | Tooth Brush | 2 | 3
Which can be usefull if you want to split the data into three tables
Customer (customer_id, customer_name)
Product (product_id, product_name)
Customer_Product(customer_id, product_id)
Is there a function in C++ that comes close to what is achieved with DENSE_RANK() in SQL ?
If a range over a structure, with a field for every column in C++ can be seen as an equivalent of a table in SQL.
With hindsight, after the question has been closed, here is the answer:
#include <vector>
#include <set>
#include <unordered_map>
#include <ranges>
using namespace std;
int main(){
const vector<int> v = {7,9,9,7};
const auto dense_rank = views::zip(v | ranges::to<set>(),views::iota(0)) | ranges::to<unordered_map>() ;
return 0;
}
Unfortunately, it requires implicit conversion from tuple to pair, so it does not compile with g++14 at the moment.
No, there isn't a function in std
like that. The range algorithms that are even close have a prerequisite that the input range be sorted in the desired order, and there isn't a sorted_view
to get the desired orders from your data.