Last active
February 23, 2020 11:06
-
-
Save plusangel/cad32911b81dc98e6789b5fdb26debc6 to your computer and use it in GitHub Desktop.
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.15) | |
project(sets) | |
set(CMAKE_CXX_STANDARD 17) | |
add_executable(sets sets.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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++) { | |
arguments.emplace_back(argv[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; | |
else | |
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