读书人

帮忙见见这个程序

发布时间: 2013-01-07 10:02:24 作者: rapoo

帮忙看看这个程序


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template <class ForwardIterator, class T>
void iota_n(ForwardIterator first, int n,T value)
{
for (int i=0;i<n;i++)
*first++=value++;
}
void main(int argc,char* argv[])
{
int n=2;
vector<int> v;
v.reserve(3);
iota_n(v.begin(),n,100);
random_shuffle(v.begin(),v.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}



应该随机输出1到100的整数,结果却什么也没有输出,求真相。
[解决办法]

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template <class ForwardIterator, class T>
void iota_n(ForwardIterator first, int n,T value)
{
for (int i=0;i<n;i++)
*first++=value++;
}
void main(int argc,char* argv[])
{
int n=2;
vector<int> v(3);
iota_n(v.begin(),n,100);
random_shuffle(v.begin(),v.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}

or the following way

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template <class ForwardIterator, class T>
void iota_n(ForwardIterator first, int n,T value)
{
for (int i=0;i<n;i++)
*first++=value++;
}
void main(int argc,char* argv[])
{
int n=2;
vector<int> v;
v.resize(3)
iota_n(v.begin(),n,100);
random_shuffle(v.begin(),v.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}

读书人网 >C++

热点推荐