如何从 win32 应用程序中的命令行参数获取 std::string?

发布于 2024-10-01 08:20:59 字数 139 浏览 1 评论 0原文

所以现在我有一个

int main (int argc, char *argv[]){}

如何使其基于字符串? int main (int argc, std::string *argv[]) 就足够了吗?

So now I have a

int main (int argc, char *argv[]){}

how to make it string based? will int main (int argc, std::string *argv[]) be enough?

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

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

发布评论

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

评论(6

一个人练习一个人 2024-10-08 08:20:59

您无法更改 main 的签名,因此这是您最好的选择:

#include <string>
#include <vector>

int main(int argc, char* argv[])
{
    std::vector<std::string> params(argv, argv+argc);
    // ...
    return 0;
}

You can't change main's signature, so this is your best bet:

#include <string>
#include <vector>

int main(int argc, char* argv[])
{
    std::vector<std::string> params(argv, argv+argc);
    // ...
    return 0;
}
说不完的你爱 2024-10-08 08:20:59

如果您想根据传递的输入参数创建一个字符串,您也可以
添加字符指针自己创建字符串

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{

string passedValue;
for(int i = 1; i < argc; i++)
 passedValue += argv[i];
    // ...
    return 0;
}

If you want to create a string out of the input parameters passed, you can also
add character pointers to create a string yourself

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{

string passedValue;
for(int i = 1; i < argc; i++)
 passedValue += argv[i];
    // ...
    return 0;
}
平生欢 2024-10-08 08:20:59

你不能那样做,因为 main 函数是显式声明的,因为它是作为入口点的。请注意,CRT 对 STL 一无所知,因此无论如何都会呕吐。尝试:

#include <string>
#include <vector>

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

    // ...

    return(0);
}; // eo main

You can't do it that way, as the main function is declared explicitly as it is as an entry point. Note that the CRT knows nothing about STL so would barf anyway. Try:

#include <string>
#include <vector>

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

    // ...

    return(0);
}; // eo main
黑色毁心梦 2024-10-08 08:20:59

这将是非标准的,因为 3.6.1 中的标准说

实现不应预定义主函数。该函数不得重载。它应具有 int 类型的返回类型,但除此之外,其类型是实现定义的。所有实现都应允许以下两个 main 定义:

int main() { /* ... */ }

int main(int argc, char* argv[]) { /* ... */ }

That would be non-standard because the Standard in 3.6.1 says

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

叶落知秋 2024-10-08 08:20:59

不,这是不允许的。如果存在,则强制为 char *argv[]。

顺便说一句,在 C++ 中 main 应始终声明为返回“int”

No. That is not allowed. If present, it is mandated to be char *argv[].

BTW, in C++ main should always be declared to return 'int'

总以为 2024-10-08 08:20:59

main 接收 char*。您必须自己将 argv 数组放入 std::strings 中。

main receives char*. you will have to put the argv array into std::strings yourself.

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