example4_args_with_STL.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <word1> [word2] [word3] ..." << std::endl;
        return 1;
    }

    std::vector<std::string> words(argv + 1, argv + argc);

    std::cout << "Original words:" << std::endl;
    for (const auto& word : words) {
        std::cout << word << " ";
    }
    std::cout << std::endl;

    std::sort(words.begin(), words.end());

    std::cout << "Sorted words:" << std::endl;
    for (const auto& word : words) {
        std::cout << word << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to command_line_arguments