C++———Vector


 1 #include<algorithm>
 2 #include <vector>
 3 #include <iostream>
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include <conio.h>
 7 using namespace std;
 8 typedef struct rect
 9 {
10     int id;
11     int length;
12     int width;
13 
14 //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。
15     bool operator< (const rect &a)  const
16     {
17         if(id!=a.id)
18             return id<a.id;
19         else
20         {
21         if(length!=a.length)
22             return length<a.length;
23         else
24             return width<a.width;
25         }
26     }
27 }Rect;
28 
29 int main()
30 {
31     std::vector<int> vecc;
32     for(int i=0;i<10;i++){
33         vecc.push_back(i);
34     }
35     
36     cout<<vecc[3]<<endl;
37     vector<int> ::iterator itt;
38     for(itt=vecc.begin();itt!=vecc.end();itt++){
39         cout<<*itt<<endl;
40     }
41     vecc.insert(vecc.begin()+3,9);
42     cout<<vecc[3]<<endl;
43     vecc.erase(vecc.begin()+3);
44     cout<<vecc[3]<<endl;
45 
46     vector<Rect> vec;
47     Rect rect;
48     rect.id=1;
49     rect.length=2;
50     rect.width=3;
51 
52     vec.push_back(rect);
53     
54     vector<Rect>::iterator it=vec.begin();
55     cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl;   
56     
57     
58     getch();
59     return 0;
60 }

 

 1 #include "opencv2/objdetect.hpp"                                                                                   
 2 #include "opencv2/videoio.hpp"
 3 #include "opencv2/highgui.hpp"
 4 #include "opencv2/imgproc.hpp"
 5 #include <iostream>
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 #include <conio.h>
 9 using namespace std;
10 using namespace cv;
11 int main()
12 {
13     std::vector<int> vec;
14     vec.push_back(1);
15     vec.push_back(2);
16     vec.push_back(3);
17     vec.push_back(4);
18     cout<<vec[3]<<endl;
19     vector<int>::iterator it;
20     for(it=vec.begin();it!=vec.end();it++){
21             cout<<"vector:"<<*it<<endl;
22     }
23     vec.insert(vec.begin()+3,9);
24     cout<<vec[3]<<endl;
25     vec.erase(vec.begin()+3);
26     cout<<vec[3]<<endl;
27     cout<<vec.size()<<endl;
28 
29     getch();
30     return 0;
31 }

 

 

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<iostream>
 5 using namespace std;
 6 
 7 typedef struct rect
 8 {
 9     int id;
10     int length;
11     int width;
12 
13   //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。
14   bool operator< (const rect &a)  const
15     {
16         if(id!=a.id)
17             return id<a.id;
18         else
19         {
20             if(length!=a.length)
21                 return length<a.length;
22             else
23                 return width<a.width;
24         }
25     }
26 }Rect;
27 
28 int main()
29 {
30     vector<Rect> vec;
31     Rect rect;
32     rect.id=1;
33     rect.length=2;
34     rect.width=3;
35     vec.push_back(rect);
36     vector<Rect>::iterator it=vec.begin();
37     cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl;    
38 
39 return 0;
40 
41 }

 

posted on 2019-04-18 23:13  AI大道理  阅读(256)  评论(0编辑  收藏  举报

导航