9w5:第九周程序填空题1

描述

下面的程序输出结果是:

1 2 6 7 8 9

请填空:

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main() {
    int a[] = {8,7,8,9,6,2,1};
// 在此处补充你的代码
    ostream_iterator<int> o(cout," ");
    copy( v.begin(),v.end(),o);
    return 0;
}

输入无输出1 2 6 7 8 9样例输入

样例输出

1 2 6 7 8 9

Approach #1:

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main() {
    int a[] = {8,7,8,9,6,2,1};
// 在此处补充你的代码
    int len = sizeof(a)/sizeof(int);
    set<int> v(a, a+len);
    ostream_iterator<int> o(cout," ");
    copy( v.begin(),v.end(),o);
    return 0;
}

  

Analysis:

看到头文件中有set,很自然地就想到了用set来做这道题。刚开始想的是用set<int> v(a, a+7)来初始化set,但是这种方法,感觉太低效,换个数组就不能用了,所以就改成了用sizeof(a)/sizeof(int)来确定数组的大小。

永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/10336373.html