-7

如何在 C++ 中输入一个数组?input - 2 3 56. 需要存入数组 A[0]=2, A[1]=3, A[2]=56?

4

2 回答 2

4
vector<int> v;
copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v));

Or if you can do it at the time the vector is constructed, it's just one line (thanks to @chris):

vector<int> v(istream_iterator<int>(cin), istream_iterator<int>());
于 2013-01-13T12:49:05.920 回答
0

Simpler to comprehend that the one with back_inserter:

std::vector<int> V;
int Temp;

while (cin >> Temp)
    V.push_back(Temp);

Note that we aren't using statically allocated array, because you didn't really specify how many elements you are going to read, and in that case it's usually better and safer to use vector.

于 2013-01-13T12:50:25.730 回答