[英]How to make user input declare the size of string array?
就像是
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
nr++; //how many lines in a text file
}
}
string y[nr] = {};
仅当我指定y [10]之类的实际数字时才有效。
除非使用非可移植的编译器扩展,否则不能声明大小未知的数组。 完成此操作的标准方法是使用vector 。
int x;
cout << "Size of your array: ";
cin >> x;
vector<string> y(x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.