Save argv to vector or string

后端 未结 2 808
面向向阳花
面向向阳花 2020-12-16 19:36

I need to save all arguments to a vector or something like this. I\'m not a programmer, so I don\'t know how to do it, but here\'s what I\'ve got so far. I just want to call

相关标签:
2条回答
  • 2020-12-16 20:21

    i need to save all arguments to a vector or something

    You can use the range constructor of the vector and pass appropriate iterators:

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

    Not 100% sure if that's what you were asking. If not, clarify.

    0 讨论(0)
  • 2020-12-16 20:30

    To build string with all argument concatenated and then run a command based on those arguments, you can use something like:

    #include <string>
    using namespace std;
    string concatenate ( int argc, char* argv[] )
    {
        if (argc < 1) {
            return "";
        }
        string result(argv[0]);
        for (int i=1; i < argc; ++i) {
            result += " ";
            result += argv[i];
        }
        return result;
    }
    int main ( int argc, char* argv[] )
    {
        const string arguments = concatenate(argc-1, argv+1);
        if (arguments == "/all /renew") {
            const string program = "c:\\windows\\system32\\ipconfig.exe";
            const string command = program + " " + arguments;
            system(command.c_str());
        } else {
            system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
        }
    }
    
    0 讨论(0)
提交回复
热议问题