parsing argc and argv in c++

孤人 提交于 2019-12-10 15:25:44

问题


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)?

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.


回答1:


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);

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




回答2:


Are you having trouble converting argv's to strings? You can just do:

string s(argv[i]);

...where i is a valid index into argv.



来源:https://stackoverflow.com/questions/1814756/parsing-argc-and-argv-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!