C++如何将argv保存为vector作为字符串

Bra*_*sen 4 c++ string vector argv visual-c++

我试图将argv保存为矢量作为字符串,但我不断收到错误: see reference to function template instantiation 'std::vector<_Ty>::vector<_TCHAR*[]>(_Iter,_Iter)' being compiled

我已经尝试将argv保存到矢量或字符串,但它不起作用

我正在使用Microsoft Visual Studio 2010.

这是我的代码:

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::string> allArgs(argv + 1, argv + argc);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

coy*_*508 5

问题是std::string没有_TCHAR*类型的构造函数,因此您无法从数组中生成字符串向量_TCHAR*.

尝试使用@NathanOliver所说的主要的"正常"版本:int main(int argc, char *argv[]).

或切换到std::wstring.

注意:如果没有启用unicode进行编译,_TCHAR*则可能等效,char *代码不会引发任何编译器错误.