How to pass in command line arguments when using ideone?

前端 未结 2 2092
陌清茗
陌清茗 2021-02-13 07:36

I\'m using the ideone online interpreter (http://ideone.com/) to test some C++ and Python programs. How do I specify the command line arguments instead of using the STDIN input?

2条回答
  •  借酒劲吻你
    2021-02-13 07:39

    Looks like you can't, but a quick hack should do the trick:

    static char * const ARGV[] = { "myprog", "hello", "world", NULL };
    
    int main(int argc, char * argv[])
    {
        argc = 3;
        argv = ARGV;
    
        // ...
    }
    

    Or convert the standard input into args:

    #include 
    #include 
    #include 
    #include 
    
    std::vector fabricate(std::vector & v)
    {
        std::vector res(v.size() + 1, NULL);
        for (std::size_t i = 0; i != v.size(); ++i) { res[i] = &v[i][0]; }
        return res;
    }
    
    std::vector args_vector((std::istream_iterator(std::cin)), std::istream_iterator());
    
    std::vector argv_vector = fabricate(args_vector);
    
    
    int main(int argc, char * argv[])
    {
        argc = args_vector.size();
        argv = argv_vector.data();
    
        // ...
    }
    

提交回复
热议问题