当使用ideone时如何传递命令行参数? [英] How to pass in command line arguments when using ideone?

查看:196
本文介绍了当使用ideone时如何传递命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ideone在线翻译( http://ideone.com/ )测试一些C ++和Python程序。如何指定命令行参数,而不是使用STDIN输入?

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?

推荐答案

看起来你不能,但是快速黑客 p>

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;

    // ...
}

标准输入到args:

#include <vector>
#include <string>
#include <iterator>
#include <iostream>

std::vector<char *> fabricate(std::vector<std::string> & v)
{
    std::vector<char *> res(v.size() + 1, NULL);
    for (std::size_t i = 0; i != v.size(); ++i) { res[i] = &v[i][0]; }
    return res;
}

std::vector<std::string> args_vector((std::istream_iterator<std::string>(std::cin)), std::istream_iterator<std::string>());

std::vector<char *> argv_vector = fabricate(args_vector);


int main(int argc, char * argv[])
{
    argc = args_vector.size();
    argv = argv_vector.data();

    // ...
}

这篇关于当使用ideone时如何传递命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆