在优化程序的效率时,应当先找出限制效率的“瓶颈”

在优化程序的效率时,应当先找出限制效率的“瓶颈”,不要在无关 紧要之处优化。

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 typedef vector<int> INTVECTOR;
 6 
 7 //测试vector容器的功能
 8 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 9 
10 int main(int argc, char** argv) {
11         //vec1对象初始为空
12     INTVECTOR vec1;   
13     //vec2对象最初有10个值为6的元素  
14     INTVECTOR vec2(10,6);  
15     //vec3对象最初有3个值为6的元素  
16     INTVECTOR vec3(vec2.begin(),vec2.begin()+3);  
17 
18     //声明一个名为i的双向迭代器
19     INTVECTOR::iterator i;
20 
21     //从前向后显示vec1中的数据
22     cout<<"vec1.begin()--vec1.end():"<<endl;
23     for (i =vec1.begin(); i !=vec1.end(); ++i)
24         cout << *i << " ";
25     cout << endl;
26 
27     //从前向后显示vec2中的数据
28     cout<<"vec2.begin()--vec2.end():"<<endl;
29     for (i =vec2.begin(); i !=vec2.end(); ++i)
30         cout << *i << " ";
31     cout << endl;
32 
33     //从前向后显示vec3中的数据
34     cout<<"vec3.begin()--vec3.end():"<<endl;
35     for (i =vec3.begin(); i !=vec3.end(); ++i)
36         cout << *i << " ";
37     cout << endl;
38  
39     //测试添加和插入成员函数
40     vec1.push_back(2);
41     vec1.push_back(4);
42     vec1.insert(vec1.begin()+1,5);
43     vec1.insert(vec1.begin()+1,vec3.begin(),vec3.end());
44     cout<<"push() and insert():" <<endl;
45     for (i =vec1.begin(); i !=vec1.end(); ++i)
46         cout << *i << " ";
47     cout << endl;
48 
49     //测试赋值成员函数
50     vec2.assign(8,1);
51     cout<<"vec2.assign(8,1):" <<endl;
52     for (i =vec2.begin(); i !=vec2.end(); ++i)
53         cout << *i << " ";
54     cout << endl;
55 
56     //测试引用类函数
57     cout<<"vec1.front()="<<vec1.front()<<endl;
58     cout<<"vec1.back()="<<vec1.back()<<endl;
59     cout<<"vec1.at(4)="<<vec1.at(4)<<endl;
60     cout<<"vec1[4]="<<vec1[4]<<endl;
61 
62     //测试移出和删除
63     vec1.pop_back();
64     vec1.erase(vec1.begin()+1,vec1.end()-2);
65     cout<<"vec1.pop_back() and vec1.erase():" <<endl;
66     for (i =vec1.begin(); i !=vec1.end(); ++i)
67         cout << *i << " ";
68     cout << endl;
69 
70     //显示序列的状态信息
71     cout<<"vec1.capacity(): "<<vec1.capacity()<<endl;
72     cout<<"vec1.max_size(): "<<vec1.max_size()<<endl;
73     cout<<"vec1.size(): "<<vec1.size()<<endl;
74     cout<<"vec1.empty(): "<<vec1.empty()<<endl;
75 
76     //vector序列容器的运算
77     cout<<"vec1==vec3: "<<(vec1==vec3)<<endl;
78     cout<<"vec1<=vec3: "<<(vec1<=vec3)<<endl;
79     return 0;
80 }
原文地址:https://www.cnblogs.com/borter/p/9417915.html