Max*_*rai 0 c++ vector std fill
I can't understand why does vector empty after it's filling.
The code is:
bool fillArray (vector<int> &array)
{
string temp;
getline(cin, temp);
if (temp == "-1")
return false
else
return true;
int res = atoi(temp.c_str());
array.push_back(res);
}
void showArray(const vector<int> array)
{
for (int i = 0; i < array.size(); i ++)
cout << array[i] << " ";
}
int main(int argc, char** argv)
{
vector<int> array;
while (fullArray (array))
{}
showArray(array);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
When I input -1 the cycle breaks but the size of vector is 0, why?
这些行是你的问题:
if (temp == "-1")
return false
else
return true;
int res = atoi(temp.c_str());
array.push_back(res);
Run Code Online (Sandbox Code Playgroud)
在输入良好的情况下,在实际使用向量上的值调用之前,您true
将从fillArray
方法返回push_back
.