在C ++中解析argc和argv [英] parsing argc and argv in c++

查看:121
本文介绍了在C ++中解析argc和argv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解更多C ++ ...通常我创建一个for循环来解析argv,最后得到了一堆C风格的字符串.我想在C ++中做类似的事情,但最好不要从/proc/任何地方读取.最初,我尝试将C样式的字符串转换为C ++样式的字符串而没有结果...令人沮丧的是,SO上的每个人似乎都想知道如何走另一条路,这就是c_str()的作用. .什么是C ++做到这一点的好方法(即解析argv)?

I want to learn more C++... Usually I make a for loop to parse argv, and I wind up with a bunch a C-style strings. I want to do something similar in C++, but preferably without reading from /proc/whatever. At first, I tried to convert the C-style string to a C++ style string without results... The frustrating bit is that everyone on SO seems to want to know how to go the other way, which is what c_str() is for. What's a good C++ way to do this (ie parse argv)?

另外,请注意,我正在寻找一种Unix风格的答案,我所见过的所有转换技术都与Windows有关,而我对此深信不疑.

Also, one note, I'm looking for a unix style answer, all the techniques for conversion I've seen have to do with Windows, which I'm completely uniterested in.

推荐答案

我不确定我是否完全理解这个问题.

I'm not sure I fully understand the question.

我知道在一个易于使用的数组中获取所有参数的最干净的方法是:

The cleanest method I know to get all the arguments in an easy to use array is:

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

但是,如果您正在寻找一种真正解析数据的方法,请查看 Boost.ProgramOptions .

But if you're looking for a way to really parse the data, check out Boost.ProgramOptions.

这篇关于在C ++中解析argc和argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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