简体   繁体   English

我可以使用 std::string* argv 作为主要 function 参数吗?

[英]Can I use std::string* argv as main function argument?

Usually I see:通常我会看到:

int main(int argc, char** argv)

But can I use:但我可以使用:

int main(int argc, std::string* argv)

instead?反而?

If it's not possible how can I convert char* to std::string ?如果不可能,我如何将char*转换为std::string

PS: I am using C++11 . PS:我正在使用C++11

main argument must be char** argv or char* argv[] . main参数必须是char** argvchar* argv[]

[basic.start.main] [基本.start.main]

2. An implementation shall not predefine the main function. 2. 一个实现不应预定义main function。 This function shall not be overloaded.此 function 不得超载。 Its type shall have C++ language linkage and it shall have a declared return type of type int , but otherwise its type is implementation-defined.其类型应具有 C++ 语言链接,并且应具有声明的返回类型int ,否则其类型是实现定义的。 An implementation shall allow both一个实现应允许两者

(2.1). (2.1)。 a function of () returning int and ()的 function 返回int

(2.2). (2.2)。 a function of (int, pointer to pointer to char) returning int (int, pointer to pointer to char)的function返回int


What you can do is to convert the arguments in the code by using one of the following:您可以做的是使用以下方法之一转换代码中的 arguments:

  • Simply assigning it:简单地分配它:
    if(argc > 1)
    { 
        std::string str = argv[1]; 
    }
  • Constructing the string using the argument as the constructor parameter:使用参数作为构造函数参数构造字符串:
    std::string str(argv[1]);
  • With initializer list:使用初始化列表:
    std::string str{argv[1]};

If you'd like to keep a collection of the arguments I would suggest keeping them in a variable size container std::vector using one of the following:如果您想保留 arguments 的集合,我建议您使用以下方法之一将它们保存在可变大小的容器std::vector中:

  • Constructing vector in place using std::vector constructor, here using pointers to the beginning and to one past the end the array of strings argv as constructor arguments:使用std::vector构造函数就地构造向量,这里使用指向字符串数组argv开头和结尾的指针作为构造函数 arguments:
    std::vector<std::string> strs(&argv[1], &argv[argc]); //*
  • Using initializer list:使用初始化列表:
    std::vector<std::string> strs{&argv[1], &argv[argc]}; //*
  • Adding the arguments in a loop:在循环中添加 arguments:
    std::vector<std::string> strs;

    for(int i = 1; i < argc; i++) //*
        strs.push_back(argv[i]);   
    }

*Not including argv[0] program name. *不包括argv[0]程序名称。

No, but there has been some discussion of this in standards committee不,但标准委员会对此进行了一些讨论

In Paper P0781 , Erich Keane suggests having the standard allow for the following:在论文P0781中,Erich Keane 建议让标准允许以下内容:

int main(const some_container<const some_string_type> args){
  for (auto Arg : args) {
    // some usage of this character array...
  }
}

There's the question of which container and which string type should be used.存在应该使用哪个容器和哪个字符串类型的问题。 Mr. Keane suggests an initializer_list of string_view 's - not std::string 's, because the latter ones require a bunch of resources, while string_view 's are very lightweight. Keane 先生建议使用string_viewinitializer_list - 而不是std::string ,因为后者需要大量资源,而string_view非常轻量级。

No, but you can create a std::vector<std::string> by using the vector constructor that takes iterators:不,但是您可以使用带有迭代器的vector构造函数来创建std::vector<std::string>

template< class InputIt >
vector( InputIt first, InputIt last, const Allocator& alloc = Allocator());

So to put the arguments (excluding the program name) in a vector :因此,将 arguments(不包括程序名称)放在一个vector中:

std::vector<std::string> args(argv + 1, argv + argc);

To separate your ordinary main from your enhanced main you could do it like this:要将您的普通main与增强main服务器分开,您可以这样做:

#include <iostream>
#include <string>
#include <vector>

int cppmain(std::string program, std::vector<std::string> args) {
    std::cout << program << " got " << args.size() << " argument(s):\n";

    for(auto& arg : args) { // each argument as a string
        std::cout << " " << arg << '\n';
    }
    return 0;
}

int main(int argc, char* argv[]) {
    //
    //         string from char*
    //                |
    //                V
    return cppmain(argv[0], {argv + 1, argv + argc});
    //                      ^                     ^
    //                      |                     |
    //                     vector<string> from char*[]
}

No, you can't.不,你不能。 But you can use something like the following to store them as std::string s但是您可以使用类似以下的内容将它们存储为std::string s

#include <iostream>
#include <string>
#include <vector>

int main(int argc,char** argv){
    std::vector<std::string> strings(argc-1);
    for(size_t i{}; i < strings.size();++i){
        strings[i] = std::string{argv[i+1]};
    }
    for(const auto& el :strings)
        std::cout << el << " ";


    return 0;
}

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

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