zoukankan      html  css  js  c++  java
  • 不要追求紧凑的代码

    不要追求紧凑的代码,因为紧凑的代码并不能产生高效的机器码。

     1 #include <iostream>
     2 #include <set>
     3 
     4 using namespace std;
     5 
     6 //创建set模板的实例
     7 typedef set<int> SET_INT;
     8 
     9 //put_HTset函数,从头向尾显示set容器的所有元素
    10 void put_HTset(SET_INT set1,char *name)
    11 {
    12     SET_INT::iterator it;
    13 
    14     cout<<name<<": ";
    15     cout<<"Head to Tail=";
    16     for (it=set1.begin();it!=set1.end();++it)
    17         cout<<(*it)<<" ";
    18     cout<<endl;
    19 }
    20 
    21 //put_THset函数,从尾向头显示set容器的所有元素
    22 void put_THset(SET_INT s1,char *name)
    23 {
    24     SET_INT::reverse_iterator i;
    25 
    26     cout<<name<<": ";
    27       cout<<"Tail to Head=";
    28     for (i=s1.rbegin(); i!=s1.rend();i++)
    29         cout <<(*i) <<" ";
    30     cout<<endl;
    31 }
    32 
    33 //测试set模板
    34 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    35 
    36 int main(int argc, char** argv) {
    37         int i;
    38     //声明set的对象和迭代器
    39     SET_INT s1;      //容器初始尾空
    40     SET_INT::iterator it;
    41 
    42     //向s1对象中插入值
    43     for (i=1;i<20;i=i+2) {
    44         s1.insert(i);
    45     }
    46 
    47     //正向显示s1中的数据
    48     put_HTset(s1,"s1");
    49 
    50     //反向显示s1中的数据
    51     put_THset(s1,"s1");
    52 
    53     //构造含有元素的序列并显示
    54     SET_INT s2(s1);
    55     put_HTset(s2,"s2");
    56 
    57     //删除s2的第2个元素并显示
    58     s2.erase(++s2.begin());
    59     put_HTset(s2,"s2");
    60 
    61     //向s2插入8和9并显示
    62     s2.insert(8);
    63     s2.insert(9);
    64     put_HTset(s2,"s2");
    65 
    66     //清空s2的序列
    67     s2.clear();
    68     put_HTset(s2,"s2");
    69 
    70     //按关键给定的区间显示序列中的元素
    71     cout<<"[s1.lower_bound(5),s1.upper_bound(15)] :";
    72     for (it=s1.lower_bound(4);it!=s1.upper_bound(16);it++)
    73         cout<<(*it)<<" ";
    74     cout<<endl;
    75 
    76     //显示s1的状态信息
    77     cout<<"s1.size():"<<s1.size()<<endl;
    78     cout<<"s1.max_size():"<<s1.max_size()<<endl;
    79     cout<<"s1.count(15):"<<s1.count(15)<<endl;
    80 
    81     //交换两个set容器的元素并显示
    82     s1.swap(s2);
    83     put_HTset(s1,"s1");
    84     put_HTset(s2,"s2");
    85 
    86     //关系运算
    87     s1.insert(5);
    88     cout<<"s1>s2 = "<<(s1>s2)<<endl;
    89     return 0;
    90 }
  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/borter/p/9417942.html
Copyright © 2011-2022 走看看