简体   繁体   English

如何将向量传递给 execvp

[英]How to pass a vector to execvp

I want to pass a vector in as the second argument to execvp.我想将一个向量作为第二个参数传递给 execvp。 Is it possible?可能吗?

Yes, it can be done pretty cleanly by taking advantage of the internal array that vectors use.是的,它可以通过利用向量使用的内部数组来非常干净地完成。

This will work, since the standard guarantees its elements are stored contiguously (see https://stackoverflow.com/a/2923290/383983 )这将起作用,因为标准保证其元素是连续存储的(请参阅https://stackoverflow.com/a/2923290/383983

#include <vector>

using namespace std;

int main(void) {
  vector<char *> commandVector;

  // do a push_back for the command, then each of the arguments
  commandVector.push_back("echo");
  commandVector.push_back("testing");
  commandVector.push_back("1");
  commandVector.push_back("2");
  commandVector.push_back("3");  

  // push NULL to the end of the vector (execvp expects NULL as last element)
  commandVector.push_back(NULL);

  // pass the vector's internal array to execvp
  char **command = &commandVector[0];

  int status = execvp(command[0], command);
  return 0;
}

Not directly;不直接; you'd need to represent the vector as a NULL-terminated array of string pointers somehow.您需要以某种方式将向量表示为以 NULL 结尾的字符串指针数组。 If it's a vector of strings, that is straightforward to do;如果它是一个字符串向量,那很简单; if it's some other kind of data you would have to figure how to encode it as strings.如果是其他类型的数据,您必须弄清楚如何将其编码为字符串。

Yes, it can be done pretty cleanly by taking advantage of the internal array that vectors use.是的,它可以通过利用向量使用的内部数组来非常干净地完成。

This will work, since the standard guarantees its elements are stored contiguously (see https://stackoverflow.com/a/2923290/383983 )这将起作用,因为标准保证其元素是连续存储的(请参阅https://stackoverflow.com/a/2923290/383983

#include <vector>

using std::vector;

int main() {
  vector<char*> commandVector;

  // do a push_back for the command, then each of the arguments
  commandVector.push_back(const_cast<char*>("echo"));
  commandVector.push_back(const_cast<char*>("testing"));
  commandVector.push_back(const_cast<char*>("1"));
  commandVector.push_back(const_cast<char*>("2"));
  commandVector.push_back(const_cast<char*>("3"));

  // push NULL to the end of the vector (execvp expects NULL as last element)
  commandVector.push_back(NULL);

  int status = execvp(command[0], &command[0]);
  return 0;
}

Do a const_cast to avoid the "deprecated conversion from string constant to 'char*'".执行 const_cast 以避免“从字符串常量到 'char*' 的不推荐转换”。 String literals are implemented as 'const char*' in C++.字符串文字在 C++ 中实现为“const char*”。 const_cast is the safest form of cast here, as it only removes the const and does not do any other funny business. const_cast 是这里最安全的强制转换形式,因为它只删除了 const 而不会做任何其他有趣的事情。 execvp will not edit the values anyway. execvp 无论如何都不会编辑这些值。

If you want to avoid all casts, you have to complicate this code by copying all the values to 'char*' types not really worth it.如果您想避免所有强制转换,您必须通过将所有值复制到不值得的 'char*' 类型来使此代码复杂化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM