64)vertor 简单使用

1)简单 代码样例:我的理解   vector  其实就是一个简单的数组,然后通过迭代器来进行 遍历数组中的值,而且有自带push_back()来添加元素

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5  void hanshu()
 6 {
 7  vector<int> v1;//v1是一个容器,将你的元素copy到容器中
 8  v1.push_back(3);//这个是开始,也就是v1.begin()
 9  v1.push_back(4);
10  v1.push_back(5);//这个是结束,也就是v1.end()
11  //3   4    5
12  //↑ →    →
13  for(vector<int>::iterator t=v1.begin();t!=v1.end();t++)
14  
15  {
16      //这个迭代器t就是 v1这个数组中一个元素的指针
17      //所以  取值要加*
18     cout << *t <<endl;
19     cout<<"!!!!!!!!!!!!!!!!!!"<<endl;
20  }
21 
22     
23  }
24  int main()
25  {
26  
27  hanshu();
28     return 0;
29  }

2)迭代器  有双向迭代器,也有单项迭代器。

3)算法和迭代器能进行无缝的连接

  

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 
 6  void hanshu()
 7 {
 8  vector<int> v1;//v1是一个容器,将你的元素copy到容器中
 9  v1.push_back(-1);
10  v1.push_back(3);
11  v1.push_back(4);
12  v1.push_back(5);
13  
14  //算法,算法和迭代器能进行无缝的连接
15  cout<<"~~~~~~~"<<count(v1.begin(),v1.end(),3)<<endl;
16  for(vector<int>::iterator t=v1.begin();t!=v1.end();t++)
17  
18  {
19      //这个迭代器t就是 v1这个数组中一个元素的指针
20      //所以  取值要加*
21     cout << *t <<endl;
22     cout<<"!!!!!!!!!!!!!!!!!!"<<endl;
23  }
24 
25     
26  }
27  int main()
28  {
29  
30  hanshu();
31     return 0;
32  }

然后是类的迭代器,其实子容器中,可以放任何类型的变量。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 class student
 6 {
 7 public:
 8     student(int a)
 9     {
10         age=a;
11     }
12 public:
13     void show()
14     {
15         cout<<age<<endl;
16     }
17 public:
18     int age;
19 
20 };
21  void hanshu()
22 {
23     //容器实现了数据类型  和 算法的有效分离
24     //v1是一个容器,将你的元素copy到容器中
25  vector<student> v1;
26  student s1(10);
27  student s2(20);
28  student s3(30);
29  v1.push_back(s1);
30  v1.push_back(s2);
31  v1.push_back(s3);
32 
33  
34 
35  for(vector<student>::iterator t=v1.begin();t!=v1.end();t++)
36  
37  {
38      //这个迭代器t就是 v1这个数组中一个元素的指针
39      //所以  取值要加*
40      cout << t->age <<endl;
41     cout<<"!!!!!!!!!!!!!!!!!!"<<endl;
42  }
43 
44     
45  }
46  int main()
47  {
48  
49  hanshu();
50     return 0;
51  }

我还可以存我的 对象的首地址,但是  我的容器存的类型要变了    vector<类名*>  v1

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 class student
 6 {
 7 public:
 8     student(int a)
 9     {
10         age=a;
11     }
12 public:
13     void show()
14     {
15         cout<<age<<endl;
16     }
17 public:
18     int age;
19 
20 };
21  void hanshu()
22 {
23     //容器实现了数据类型  和 算法的有效分离
24     //v1是一个容器,将你的元素copy到容器中
25  vector<student> v1;
26  student s1(10);
27  student s2(20);
28  student s3(30);
29  v1.push_back(s1);
30  v1.push_back(s2);
31  v1.push_back(s3);
32 
33  
34 
35  for(vector<student>::iterator t=v1.begin();t!=v1.end();t++)
36  
37  {
38      //这个迭代器t就是 v1这个数组中一个元素的指针
39      //所以  取值要加*
40      cout << t->age <<endl;
41     cout<<"!!!!!!!!!!!!!!!!!!"<<endl;
42  }
43 
44     
45  }
46  int main()
47  {
48  
49  hanshu();
50     return 0;
51  }
原文地址:https://www.cnblogs.com/xiaoyoucai/p/8282500.html