[英]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** argv
或char* 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
()
returningint
and()
的 function 返回int
和(2.2).
(2.2)。 a function of
(int, pointer to pointer to char)
returningint
(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:
if(argc > 1)
{
std::string str = argv[1];
}
std::string str(argv[1]);
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
中:
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]); //*
std::vector<std::string> strs{&argv[1], &argv[argc]}; //*
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]
程序名称。
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_view
的initializer_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.