STL(三)vector复杂对象的创建及遍历

来源:互联网 发布:架构师和程序员哪个好 编辑:程序博客网 时间:2024/06/14 06:05

   由上一节中了解vector基本使用,但是往往编程中使用vector复杂类型创建对象,因为泛型编程中类型的自由。

这样使得对象变得更为复杂。

1、二维数组

定义:

vector<vector<type>> obj;

#include<iostream>#include<vector>#include<string>#include<ctime>using namespace std;//二维数组遍历//迭代器void reverse_with_iterator(vector<vector<int>> vec){if (vec.empty()){cout << "The vector is empty!" << endl;return;}vector<int>::iterator it;vector<vector<int>>::iterator iter;vector<int> vec_tmp;cout << "Use iterator : " << endl;for (iter = vec.begin(); iter != vec.end(); iter++){vec_tmp = *iter;for (it = vec_tmp.begin(); it != vec_tmp.end(); it++)cout << *it << " ";cout << endl;}cout << endl;}//得到行、列大小,利用下标进行遍历void reverse_with_index(vector<vector<int>> vec){if (vec.empty()){cout << "The vector is empty!" << endl;return;}int i, j;cout << "Use index : " << endl;for (i = 0; i < vec.size(); i++){for (j = 0; j < vec[0].size(); j++)cout << vec[i][j] << " ";cout << endl;}cout << endl;}int main(){    //二维数组的实现vector<vector<int>>vec1(3);//数组的行for (int i = 0; i < 3; i++)vec1[i].resize(3);//数组的列for (int i = 0; i < vec1.size(); i++){for (int j = 0; j < vec1[0].size(); j++)vec1[i][j] = i*j;}for (int i = 0; i < vec1.size(); i++){for (int j = 0; j < vec1[0].size(); j++)cout<<vec1[i][j] <<" ";cout << endl;}cout << endl;vec1.resize(5);//调整vec1行数vec1[3].resize(3);//补充剩余行的列数vec1[4].resize(3);for (int i = 0; i < vec1.size(); i++){for (int j = 0; j < vec1[0].size(); j++)vec1[i][j] = i*j;}reverse_with_iterator(vec1);cout << endl;//或vector<vector<int>> vec2;vector<int> a2;a2.push_back(1);a2.push_back(2);a2.push_back(3);vector<int> b2;b2.push_back(4);b2.push_back(5);b2.push_back(6);vec2.push_back(a2);vec2.push_back(b2);reverse_with_index(vec2);cout << endl;system("pause");return 0;}


2、元素指针

定义:

vector<type *> obj;

相当于指针数组(即数组中的每个元素都是指针)。

元素访问形式:*obj[i];

对象obj中的每个元素的类型均是type *

#include<iostream>#include<vector>#include<string>using namespace std;//指针元素遍历template<typename T>void Output(T vec){for (int i = 0; i < vec.size(); i++){cout << *vec[i] << " ";}cout << endl;}//或template<typename T>void Output0(T vec){T::iterator iter = vec.begin();for (; iter != vec.end();){cout << **iter++ << " ";}cout << endl;}int main(){    //元素指针vector<int*> vec;//vec中的每个元素的类型是int*(存储整型的地址)int a = 8, b = 9;vec.push_back(&a);//vec[0]=&a;vec.push_back(&b);//vec[1]=&b;Output(vec);Output0(vec);        cout<<"val:"<<*vec[0]<<endl;       vector<string*>vst;string st1 = "Hello";string st2 = "world";vst.push_back(&st1);vst.push_back(&st2);Output(vst);Output0(vst);system("pause");return 0;}



3、对象指针

定义:

vector<type> *obj=new vector<type>;  必须要分配空间


取出对象中的元素:

obj[i]、*(obj[i])=*obj[i]、(*obj)[i]选择哪个?

vector<type>可以简单理解为type [ ] 

vector<type> *obj---------->type [ ] *obj,相当于数组指针。

元素访问选取(*obj)[i]


#include<iostream>#include<vector>#include<string>#include<ctime>using namespace std;int main(){//对象指针//使用方法先定义、初始化,在创建该指针前的类型的对象//遇到复杂类型的对象,需要拆开vector<int> *vec3 = new vector<int>;//创建对象指针vector<int> vec4;// 创建普通对象vec4.push_back(1);vec4.push_back(2);vec3 = &vec4;   //将普通对象的地址赋给指针cout <<(*vec3)[1]<< endl;//去除对象中的元素system("pause");return 0;}


4、创建对象指针的元素类型也为指针

定义:

vector<type*> *obj; 可以暂不分配空间。

元素访问:参考2、3点

#include<iostream>#include<vector>using namespace std;int main(){int a = 8, b = 9;//创建对象指针中元素也为指针vector<int*>*vec5;vector<int*>vec6;vec6.push_back(&a) ;vec6.push_back(&b);vec5 = &vec6;cout << *(*vec5)[1] << endl;system("pause");return 0;}

5、自定义类

#include<iostream>#include<vector>using namespace std;class Myclass{public:Myclass(){};Myclass(int n){this->n = n;}int n;void fun(){cout << "fun" << endl;}};int main(){vector<Myclass>vec;Myclass p(6);Myclass q;vec.push_back(p);cout << vec[0].n << endl;vec[0].fun();vector<Myclass*>vec1;Myclass *p1=new Myclass(1);Myclass *p2 = new Myclass(2);Myclass *p3 = new Myclass(3);vec1.push_back(p1);vec1.push_back(p2);vec1.push_back(p3);//方法1for (int i = 0; i < vec1.size(); i++){cout << vec1[i]->n << "  ";vec1[i]->fun();}cout << endl;//方法2for (int i = 0; i < vec1.size(); i++){cout << vec1.at(i)->n << "  ";vec1.at(i)->fun();}cout << endl;//方法3for (vector<Myclass*>::iterator iter = vec1.begin(); iter != vec1.end(); iter++){cout << (**iter).n << " ";(**iter).fun();}cout << endl;delete p1;delete p2;delete p3;system("pause");return 0;}



6、综合示例

#include<iostream>  #include<vector>  #include<string>  using namespace std;class Student{public:string m_strNO;string m_strName;string m_strSex;string m_strDate;public:Student(string strNO, string strName, string strSex, string strDate){m_strNO = strNO;m_strName = strName;m_strSex = strSex;m_strDate = strDate;}void Display(){cout << m_strNO << "\t";cout << m_strName << "\t";cout << m_strSex << "\t";cout << m_strDate << "\t";}};class StudCollect{vector<Student> m_vStud;public:void Add(Student &s){m_vStud.push_back(s);}Student* Find(string strNO){bool bFind = false;int i;for (i = 0; i < m_vStud.size(); i++){Student& s = m_vStud.at(i);if (s.m_strNO == strNO){bFind = true;break;}}Student *s = NULL;if (bFind)s = &m_vStud.at(i);return s;}};int main(){Student s1("1001", "zhangsan", "boy", "1988-10-10");Student s2("1002", "lisi", "boy", "1988-8-25");Student s3("1003", "wangwu", "boy", "1989-2-14");StudCollect s;s.Add(s1);s.Add(s2);s.Add(s3);Student *ps = s.Find("1002");if (ps)ps->Display();system("pause");return 0;}



待续。。。。。。。。。


原创粉丝点击