C++陣列初始化[]中可不可以填入變數?
阿新 • • 發佈:2020-12-29
技術標籤:c++
如果變數型別在前面只用int限定是無法通過編譯的;如下:
#include <iostream>
#include <vector>
using namespace std;
#include <algorithm>
int main()
{
int elem_seq = 10;
int elem_vals[elem_seq] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> v1(elem_vals, elem_vals + elem_seq) ;
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
只需前面加上const即可
const int elem_seq = 10;