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

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

问题描述

我想学习更多 C++... 通常我会创建一个 for 循环来解析 argv,最后我会得到一堆 C 风格的字符串.我想在 C++ 中做类似的事情,但最好不要从/proc/whatever 中读取.起初,我试图将 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天全站免登陆