根据字符串检查 argv[]?(C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5183203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:36:40  来源:igfitidea点击:

Checking argv[] against a string? (C++)

c++

提问by Ben

So I'm attempting to check the arguments that I'm inputting into my program, and one of them is either the word "yes" or "no", entered without the quotes.

所以我试图检查我输入到我的程序中的参数,其中之一是单词“是”或“否”,输入时不带引号。

I'm trying to test equivalency ( if (argv[n] == "yes") ) but that seems to be returning false every time when the input is, in fact, yes(When I output it it confirms this). What am I missing here that I'm doing improperly? If I understand properly argv[n] returns a cstring that is null-terminated, so it should allow me to do this.

我正在尝试测试等效性( if (argv[n] == "yes") ),但是每次输入实际上是 yes 时似乎都返回 false (当我输出它时,它确认了这一点)。我在这里错过了什么我做的不正确?如果我理解正确 argv[n] 返回一个以空字符结尾的 cstring,所以它应该允许我这样做。

回答by Erik

You're comparing pointers. Use strcmp, or std::string.

你在比较指针。使用 strcmp 或 std::string。

int main(int argc, char * argv[]) {

  if (argv[1] == "yes"); // Wrong, compares two pointers
  if (strcmp(argv[1], "yes") == 0); // This compares what the pointers point to
  if (std::string(argv[1]) == "yes"); // Works fine
  if (argv[1] == std::string("yes")); // Works fine

  // Easy-mode    
  std::vector<std::string> args(argv, argv+argc);
  for (size_t i = 1; i < args.size(); ++i) {
      if (args[i] == "yes") {
          // do something
      }
  }

}

回答by Riot

Here's a better alternative to std::string, and when efficiency is important - in C++17 you now have the very useful std::string_view. This lets you work with the arguments similarly to a std::string, without incurring the cost of copying.

这是 std::string 的更好替代方案,当效率很重要时 - 在 C++17 中,您现在拥有非常有用的std::string_view。这使您可以使用类似于 std::string 的参数,而不会产生复制成本。

Currently available in std::experimental in GCC:

目前在 GCC 的 std::experimental 中可用:

#include <experimental/string_view>
...
if(std::experimental::string_view(argv[1]) == "yes") {
  // do things
}

回答by Bernd Elkemann

if(strcmp(argv[0],"yes")==0) { // equal to "yes"

strcmp is zero if the 2 strings are the same.

如果 2 个字符串相同,则 strcmp 为零。

回答by count0

You could also take a look into boost::program_options, though this seems a little off topic and overkill, but once you get used to it it's easy, convenient and safe to use. Some advantages are auto-generated --help for your program, plus things like string evaluation can be done safe using lexical_cast.

你也可以看看boost::program_options,虽然这看起来有点离题和矫枉过正,但是一旦你习惯了它,它使用起来很容易、方便和安全。一些优点是为您的程序自动生成 --help ,此外可以使用 lexical_cast 安全地完成诸如字符串评估之类的事情。

回答by nobar

Modern C++, with a bit of constcorrectness...

现代 C++,有一点const正确性......

/*BINFMTCXX: -Wall -Werror -std=c++17
*/

   #include <iostream>
   #include <string>
   #include <vector>
   using std::string; using std::vector; using std::cerr;

int main( int argc, char * const argv[] )
   {
   assert( argc >= 1 ); // exploratory -- could fail in principle, but not really
   const vector<string> args(argv+1,argv+argc); // convert C-style to modern C++
   for ( auto a : args ) cerr<<(a=="yes")<<"\n"; // operator '==' works as expected
   }


Note: The standard doesn't guarantee that you can use constin the signature of main, nor does it forbid it.

注意:本标准不保证可以const在 的签名中使用main也不禁止

As used here, constensures that we won't change things we don't intend to change -- which is the purpose of constin the C++ language.

在这里使用,const确保我们不会改变我们不打算改变的东西——这是constC++ 语言的目的。

See also...

也可以看看...