【发布时间】:2011-06-23 15:44:47
【问题描述】:
int main()
{
const int SIZE = 10;
int a[SIZE] = {10, 2, 35, 5, 10, 26, 67, 2, 5, 10};
std::ostream_iterator< int > output(cout, " ");
std::vector< int > v(a, a + SIZE);
std::vector< int >::iterator newLastElement;
cout << "contents of the vector: ";
std::copy(v.begin(), v.end(), output);
newLastElement = std::remove(v.begin(), v.end(), 10);
cout << "\ncontents of the vector after remove: ";
//std::copy(v.begin(), newLastElement, output);
//this gives the correct result : 2 35 5 26 67 2 5
std::copy(v.begin(), v.end(), output);
//this gives a 10 which was supposed to be removed : 2 35 5 26 67 2 5 2 5 10
cout << endl;
return 0;
}
数组a中有3个10。
为什么我们用remove函数删除所有10后数组v包含一个10。
你也可以看到编译后的输出here
【问题讨论】:
-
顺便问一下,这条评论 (
//this gives the correct result : 2 35 5 26 67 2 5 2 5) 是程序真正发出的吗?你真的是想说2 35 5 26 67 2 5,对吧? -
是的,完全正确。感谢指出