C ++命令行字符串像Java? [英] C++ command line strings like Java?

查看:105
本文介绍了C ++命令行字符串像Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从命令行获取c ++字符串,就像在Java?

  public static void main args)

其中args是一个C ++字符串数组?

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

using namespace std;

typedef vector< string> CommandLineStringArgs;

int main(int argc,char * argv [])
{
CommandLineStringArgs cmdlineStringArgs(& argv [0],& argv [0 + argc]);

for(int i = 0; i {
cout< cmdlineStringArgs [i]< endl;
}

return 0;
}

这只是使用std :: vector的重载构造函数,结束迭代器对将命令行参数复制到向量中。



您可以使用实用程序方法来构建和对象该向量,以转换参数,但几乎没有意义。还有很多包对象处理解释命令行开关等。 ACE,POCO,QT等都有这样的设施。


Is there a way to get c++ strings from the commandline like in Java?

public static void main(String[] args)

where args is an array of C++ strings?

解决方案

Not precisely but you can come close easily.

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

using namespace std;

typedef vector<string> CommandLineStringArgs;

int main(int argc, char *argv[])
{
    CommandLineStringArgs cmdlineStringArgs(&argv[0], &argv[0 + argc]);

    for (int i = 0; i < cmdlineStringArgs.size(); ++i)
    {
        cout << cmdlineStringArgs[i] << endl;
    }

    return 0;
}

This just uses the overloaded constructor for std::vector that takes a begining/ending iterator pair to copy the command line arguments into the vectors. It is much the same as java from there on.

You can likewise build and object around that vector with utility methods to convert arguments but there is almost no point. Also there are plenty of packages with object that deal with interpreting command line switches and such. ACE, POCO, QT, etc.. all come with such facilities.

这篇关于C ++命令行字符串像Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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