Skip to content

Instantly share code, notes, and snippets.



Last active Feb 23, 2020
What would you like to do?
A program that accepts any number of command line arguments and prints them in alphanumerically sorted order, using a std::set<const char*> to store the elements, then iterate over the set to obtain the sorted result (custom comparator that compares two C-style strings)
cmake_minimum_required(VERSION 3.15)
add_executable(sets sets.cpp)
#include <array>
#include <cmath>
#include <iostream>
#include <set>
#include <string.h>
#include <vector>
int main(int argc, char *argv[]) {
std::cout << "Number of arguments " << argc - 1 << std::endl;
std::vector<const char *> arguments{};
for (size_t i{1}; i < argc; i++) {
std::cout << "The vector " << std::endl;
for (auto &x : arguments)
std::cout << x << " ";
auto cmp = [](const char *a, const char *b) {
if (strcmp(a, b) < 0)
return true;
return false;
std::set<const char *, decltype(cmp)> my_set{arguments.cbegin(),
arguments.cend(), cmp};
std::cout << "\nThe set " << my_set.size() << std::endl;
for (auto it = my_set.begin(); it != my_set.end(); it++)
std::cout << *it << " ";
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment