• 多花费一些内存来提高性能


    有时候时间效率和空间效率可能对立,此时应当分析那个更重要, 作出适当的折衷。

    例如多花费一些内存来提高性能。

     1 #include <iostream>
     2 #include <list>
     3 
     4 using namespace std;
     5 typedef list<int> INTLIST;
     6 
     7 //从前向后显示list队列的全部元素
     8 void put_list(INTLIST list, char *name)
     9 {
    10     INTLIST::iterator plist;
    11 
    12     cout << "The contents of " << name << " : ";
    13     for(plist = list.begin(); plist != list.end(); plist++)
    14         cout << *plist << " ";
    15     cout<<endl;
    16 }
    17 
    18 //测试list容器的功能
    19 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    20 
    21 int main(int argc, char** argv) {
    22         //list1对象初始为空
    23     INTLIST list1;   
    24     //list2对象最初有10个值为6的元素  
    25     INTLIST list2(10,6);  
    26     //list3对象最初有3个值为6的元素  
    27     INTLIST list3(list2.begin(),--list2.end());  
    28 
    29     //声明一个名为i的双向迭代器
    30     INTLIST::iterator i;
    31 
    32     //从前向后显示各list对象的元素
    33     put_list(list1,"list1");
    34     put_list(list2,"list2");
    35     put_list(list3,"list3");
    36     
    37     //从list1序列后面添加两个元素
    38     list1.push_back(2);
    39     list1.push_back(4);
    40     cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
    41     put_list(list1,"list1");
    42 
    43     //从list1序列前面添加两个元素
    44     list1.push_front(5);
    45     list1.push_front(7);
    46     cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
    47     put_list(list1,"list1");
    48 
    49     //在list1序列中间插入数据
    50     list1.insert(++list1.begin(),3,9);
    51     cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;
    52     put_list(list1,"list1");
    53 
    54     //测试引用类函数
    55     cout<<"list1.front()="<<list1.front()<<endl;
    56     cout<<"list1.back()="<<list1.back()<<endl;
    57 
    58     //从list1序列的前后各移去一个元素
    59     list1.pop_front();
    60     list1.pop_back();
    61     cout<<"list1.pop_front() and list1.pop_back():"<<endl;
    62     put_list(list1,"list1");
    63 
    64     //清除list1中的第2个元素
    65     list1.erase(++list1.begin());
    66     cout<<"list1.erase(++list1.begin()):"<<endl;
    67     put_list(list1,"list1");
    68 
    69     //对list2赋值并显示
    70     list2.assign(8,1);
    71     cout<<"list2.assign(8,1):"<<endl;
    72     put_list(list2,"list2");
    73 
    74     //显示序列的状态信息
    75     cout<<"list1.max_size(): "<<list1.max_size()<<endl;
    76     cout<<"list1.size(): "<<list1.size()<<endl;
    77     cout<<"list1.empty(): "<<list1.empty()<<endl;
    78 
    79     //list序列容器的运算
    80     put_list(list1,"list1");
    81     put_list(list3,"list3");
    82     cout<<"list1>list3: "<<(list1>list3)<<endl;
    83     cout<<"list1<list3: "<<(list1<list3)<<endl;
    84 
    85     //对list1容器排序
    86     list1.sort();
    87     put_list(list1,"list1");
    88     
    89     //结合处理
    90     list1.splice(++list1.begin(), list3);
    91     put_list(list1,"list1");
    92     put_list(list3,"list3"); 
    93     return 0;
    94 }
  • 相关阅读:
    《大话数据结构》第9章 排序 9.9 快速排序(下)
    [HTML5 DOM] dispatchEvent
    [AWS SAP] Exam Tips 2 Continues Improvement for Existing Solutions
    遇见C++ PPL:C++ 的并行和异步
    遇见C++ AMP:在GPU上做并行计算
    遇见C++ Lambda
    遇见C++ AMP:GPU的线程模型和内存模型
    服务器推技术
    转http状态码
    extjs同步与异步请求互换
  • 原文地址:https://www.cnblogs.com/borter/p/9417936.html
Copyright © 2020-2023  润新知