标准模板库 STL-1 编程题#1(Coursera 程序设计与算法 专项课程3;用法:迭代器 ostream_iterator,容器 set)

来源:互联网 发布:专业网络公关 编辑:程序博客网 时间:2024/05/21 10:22

编程题#1

来源: POJ (http://cxsjsxmooc.openjudge.cn/test/9w5/)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述
下面的程序输出结果是:
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

程序解答

#include <iostream> #include <iterator> #include <set> using namespace std;int main() {    int a[] = { 8, 7, 8, 9, 6, 2, 1 };    // 在此处补充你的代码    set<int> v(a, a + 7);  //关联容器内不允许相同元素,插入任何元素,都按相应的排序规则来确定其位置。multiset中允许存在相同的元素    ostream_iterator<int> o(cout, " ");  //放到输出流的时候,每放一个整数,就末尾添加一个" "中的内容 http://blog.csdn.net/u012482828/article/details/72549003    copy(v.begin(), v.end(), o);  //V中的数据通过流迭代器o放到o输出流中    return 0;}
阅读全文
0 0
原创粉丝点击