C++容器vector及迭代器对象iterator

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 int main()
 7 {   vector<double> Vecdouble = {99.3,46.1,26.1,44.3};
 8     Vecdouble.push_back(100.5);
 9     for(int i = 0; i< Vecdouble.size(); i++){
10         cout << Vecdouble[i]<< endl;
11     }
12      //集合的通用遍历方法,使用迭代器iterator.
13     vector<double>::iterator it;  //得到迭代器对象,::域运算符,说明迭代器对象是vector<double>中的。
14     //迭代器对象实际上是一个指针对象。
15     for(it = Vecdouble.begin();it != Vecdouble.end(); it++){  //.begin()和.end()是迭代器,相当于迭代器对象的头指针和尾指针。
16         //在迭代器对象中it++容易导致缓存增加,效率不高。推荐用++it。
17     cout << *it << endl;
18 }
19     //排序
20     sort(Vecdouble.begin(),Vecdouble.end());
21     for(it = Vecdouble.begin();it != Vecdouble.end(); it++){
22     cout << *it << endl;
23 }
24     //逆序
25     reverse(Vecdouble.begin(),Vecdouble.end());//
26     for(it = Vecdouble.begin();it != Vecdouble.end(); it++){
27     cout << *it << endl;
28 }
29 }

 

另一个主流的较快的数组array

array<int,4> ad = {1,2,3,4}

posted @ 2019-08-26 14:42  Parallax  阅读(350)  评论(0编辑  收藏  举报