我想創建一個特定的大小,255(最大)的向量.. 它不適用於我,就像我在互聯網上看到的例子...不能創建一個指定大小的向量
我使用Microsoft Visual C++ 2012 ...
我當前的代碼:
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
const int MAX = 255;
class test
{
vector <string> Name(MAX);
};
int main()
{
system("PAUSE");
}
它給了我2個錯誤:
Error 1 error C2061: syntax error : identifier 'MAX'
2 IntelliSense: variable "MAX" is not a type name
感謝您的幫助!
在C++ 11,你可以用'矢量名稱{MAX};'但是,這隻適用於vector的情況,因爲initializer構造函數與'std :: string'的int類型不兼容。更清楚地說,使用'int'類型而不是'std :: string',向量將包含1個元素 - 而不是256個默認初始化元素。 –
chris