CodingHowTo

Command-Line Args to std::vector in Two Lines<

Convert Command-Line Args to std::vector in Two Lines

Content

Converting command-line arguments to a std::vector can be done in just two lines of code using the standard library. Here's how you can do it:


#include <vector>
#include <iostream>

int main(int argc, char* argv[]) {
    std::vector args(argv + 1, argv + argc);
    // Now you can use 'args' as a vector of strings
    for (const auto& arg : args) {
        std::cout << arg << std::endl;
    }
    return 0;
}

    

Conclusion

By using the two-line approach shown above, you can easily convert command-line arguments into a std::vector. This method leverages the power of C++'s standard library to handle inputs efficiently. With this technique, processing and manipulating command-line arguments becomes straightforward, allowing you to focus on the core functionality of your program.