处理命令行参数

发布于 2024-09-07 20:54:58 字数 223 浏览 9 评论 0原文

我一直在使用 OpenCV,我见过的一些示例代码使用以下内容来读取文件名。我知道 argc 是传递的命令行参数的数量,argv 是参数字符串的向量,但是有人可以澄清以下行的每个部分的作用吗?我尝试过搜索此内容,但没有找到很多结果。谢谢。

const char* imagename = argc > 1 ? argv[1] : "lena.jpg";

谢谢。

I've been working with OpenCV, and some of the example code I've seen uses the following to read in a filename. I understand that argc is the number of command line arguments that were passes, and argv is a vector of argument strings, but can someone clarify what each part of the following line does? I've tried searching this but haven't found many results. Thanks.

const char* imagename = argc > 1 ? argv[1] : "lena.jpg";

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凝望流年 2024-09-14 20:54:58
const char* imagename =  // assign the string to the variable 'image_name'
       argc > 1          // if there is more than one cmd line argument (the first is always the program name)
       ? argv[1]         // use the first argument after the program name
       : "lena.jpg";     // otherwise use the default name of "lena.jpg"
const char* imagename =  // assign the string to the variable 'image_name'
       argc > 1          // if there is more than one cmd line argument (the first is always the program name)
       ? argv[1]         // use the first argument after the program name
       : "lena.jpg";     // otherwise use the default name of "lena.jpg"
同尘 2024-09-14 20:54:58

如果 argc 大于 1,则将 argv[1] 中保存的指针(即命令行上给出的第一个参数)分配给 imagename;否则(argc 不大于 1),分配默认值“lena.jpg”。

它使用三元运算符?:。这是这样使用的: CONDITION ? A : B 可以读作

if (CONDITION)
  A
else
  B

So that a = C ? A : B 如果 C 为 true,则将 A 分配给 a,否则将 B 分配给 a。在本例中,“A”和“B”是指向 char (char *) 的指针; const 属性表示我们有“常量”的“字符串”。

If argc is greater than 1, assigns to imagename the pointer held in argv[1] (i.e. the first argument given on the command line); otherwise (argc is not greater than 1), assigns a default value, "lena.jpg".

It uses the ternary operator ?:. This is used this way: CONDITION ? A : B and can be read as

if (CONDITION)
  A
else
  B

So that a = C ? A : B assigns A to a if C is true, otherwise assigns B to a. In this specific case, "A" and "B" are pointers to char (char *); the const attribute says we have "strings" that are "constant".

匿名。 2024-09-14 20:54:58

该示例显示了三元运算符的使用。

const char* imagename = argc >; 1 : argv[1] : "lana.jpg"
通过三元,您可以说该表达式具有三个成员。

第一个成员是条件表达式

第二个成员是如果条件表达式为真则可以分配给 imagename 的值。

第三个成员是如果条件表达式为 false 时可以分配给 imagename 的值。

这个例子可以翻译为:

const char* imagename;
if(argc > 1)
    imagename = argv[1];
else
    imagename = "lana.jpg";

The example shows the use of the ternary operator.

const char* imagename = argc > 1 : argv[1] : "lana.jpg"
By ternary you can say that this expression has three members.

First member is a condition expression

Second member is the value that could be assigned to imagename if conditional expression is true.

Third member is the value that could be assigned to imagename if conditional expression is false.

This example can be translated to:

const char* imagename;
if(argc > 1)
    imagename = argv[1];
else
    imagename = "lana.jpg";
骄傲 2024-09-14 20:54:58
if (argc > 1) {
  const char* imagename = argv[1];
} else {
  const char* imagename = "lena.jpg";
}

(如果我们同意imagename可以超出括号的范围)

if (argc > 1) {
  const char* imagename = argv[1];
} else {
  const char* imagename = "lena.jpg";
}

(if we agree that imagename can go out of the brackets' scope)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文