问题及注意
编写程序,使用fill_n()将一个序列中的int值都设置为0.
++++++++++++++++++PS注意:++++++++++++++++++++
!!!!!!注意:
不能在一个空容器上调用fill_n(或类似的写元素的算法).
—————–>
vector
//灾难:修改vec中的10个(不存在)元素.
fill_n(vec.begin(),10,0);
<---------------end. 这个调用是一场灾难。我们指定了要写入10个元素,但vec中并没有元素————它是空的。这条语句的结果是未定义的。 +++++++++++++++++++++++++++++++++++++++++++++++++++++
源代码
/*************************************************************************
> File Name: test.10.6.cpp
> Author: puruidong
> Mail: 1@w1520.com
> Created Time: 2014年06月07日
************************************************************************/
#include
#include
#include
#include
using namespace std;
/*****************************
编写程序,使用fill_n()将一个序列中的int值都设置为0.
++++++++++++++++++PS注意:++++++++++++++++++++
!!!!!!注意:
不能在一个空容器上调用fill_n(或类似的写元素的算法).
----------------->
vector
//灾难:修改vec中的10个(不存在)元素.
fill_n(vec.begin(),10,0);
<---------------end. 这个调用是一场灾难。我们指定了要写入10个元素,但vec中并没有元素————它是空的。这条语句的结果是未定义的。 +++++++++++++++++++++++++++++++++++++++++++++++++++++ *************************************/ void outv(vector
{
for(vector
{
cout << *it << endl;
}
} int main()
{
vector
vec.push_back(0);
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
cout << "未改变之前:" << endl;
outv(vec);
fill_n(vec.begin(),vec.size(),0);
cout << "改变之后:" << endl;
outv(vec);
return 0;
}