用 std::vector<std::string> 替换命令行参数 int argc 和 char** argv; [英] replacing the command line arguments int argc and char** argv with std::vector<std::string>

查看:21
本文介绍了用 std::vector<std::string> 替换命令行参数 int argc 和 char** argv;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章之后,我找到了我的另一个问题,我想知道我是否可以用 std::vectorint argc, char** argv 替换 int argc, char** argvstd::string> 变量/对象.

Following this post, where I have found a temporary workaround for my other problem, I want to know if I can replace the int argc, char** argv with a std::vector<std::string> variable/object.

考虑想象中的代码:

#include <iostream>
#include <CloseLibrary>

void someFunction(int argc, char** argv){
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

int myFunc(int argc, char** argv){
    someFunction(argc, argv);

    return 0;
}

其中 CloseLibrary 是一个封闭的库,我无法访问源代码,而该库中的 someFunction 函数需要 int argc, char** argv 命令行参数.但是由于某些原因我不能有双指针char** 在我的代码中.

where the CloseLibrary is a closed library that I don't have access to the source code, and the someFunction function from that library demands the int argc, char** argv command line arguments. But for some reason I can't have double pointers char** in my code.

在这篇文章中 提出了类似我需要的东西,但我不知道如何使用它.我可以这样写代码吗:

Here in this post something like what I need is proposed, but I don't know how to use that. Can I write the code this way:

#include <iostream>
#include <CloseLibrary>
#include <vector>

void someFunction(int argc, char** argv){
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

int myFunc("args", [](std::vector<std::string> args){
    std::vector<char *> cstrs;
    cstrs.reserve(args.size());
    for (auto &s : args) cstrs.push_back(const_cast<char *>(s.c_str()));
    someFunction(cstrs.size(), cstrs.data());

    return 0;
}

或者也许有更规范的方法来做到这一点?如果您能帮助我找到正确的方法并理解解决方案,我将不胜感激.提前感谢您的帮助.

Or maybe there is a more canonical way to do this? I would appreciate it if you could help me find the correct way to do this and understand the solution. Thanks for your help in advance.

P.S.1. char* argv[] 方法在函数体中可以,但在输入中不可以.我不知道为什么 pybind11 这样做!

P.S.1. The char* argv[] method is ok in the body of the function but not ok in the inputs. I don't know why pybind11 does this!

PS2. 在 pybind11 gitter 上,有人建议:

void run(const std::vector<std::string>& args) {
    for(auto&& e : args) std::cout << e << '\n';
}

P.S.3. 在 pybind11 Gitter 上也有建议:

P.S.3. Also suggested on pybind11 Gitter:

char** argv = new char*[vec.size()]; // just like malloc(sizeof(char*)*vec.size());
for (int i = 0; i < vec.size(), i++) {
    argv[i] = new char[vec[i].size()];
    memcpy(argv[i], vec[i].data(), vec[i].size()); // or strcpy
}

推荐答案

您可以使用从给定范围初始化向量的构造函数,其中 argv 参数充当起始迭代器和 argv+argc 作为结束迭代器.

You could use the constructor which initializes the vector from a given range, with the argv parameter acts as the starting iterator and argv+argc acting as the ending iterator.

例如,我通常用以下代码开始我的主函数:

For example, I usually start my main function with:

int main( int argc, char* argv[] )
{
    std::vector< std::string > args( argv, argv + argc );

    for ( auto s : args )
    {
        std::cout << s << std::endl;
    }
}

请注意,这也将捕获第一个参数 (argv[0]),该参数通常(但不一定)在启动时保存应用程序的名称.

Note that this will also capture the first argument (argv[0]) which usually (but not necessarily) hold the name of the application when it is started.

在你的情况下,你想做相反的事情,从 std::vector< 建立一个 char* 的连续数组.标准::字符串>.我会做类似的事情:

In your case, you would like to do the reverse, build up a contiguous array of char* from a std::vector< std::string >. I would do something like:

std::vector< char* > rargs( args.size(), 0 ); // Initialize N nullptrs.
for ( int i=0; i<args.size(); ++i )
{
    std::strcpy( rargs[i], args[i].c_str() ); // One-by-one strcpy them 
}

然后你可以将它们传递给一个接受 argc, argv as 的函数

And then you can pass them into a function accepting an argc, argv as

someFunction( rargs.size(), rargs.data() );

这篇关于用 std::vector&lt;std::string&gt; 替换命令行参数 int argc 和 char** argv;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆