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

26 篇文章 0 订阅

   由上一节中了解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);

	//方法1
	for (int i = 0; i < vec1.size(); i++)
	{
		cout << vec1[i]->n << "  ";
		vec1[i]->fun();
	}
	cout << endl;
	//方法2
	for (int i = 0; i < vec1.size(); i++)
	{
		cout << vec1.at(i)->n << "  ";
		vec1.at(i)->fun();
	}
	cout << endl;

	//方法3
	for (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;
}



待续。。。。。。。。。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
自定义STL优化vector可以从以下几个方面进行改进: 1. 动态内存分配优化:vector是一个动态数组,当元素数量超过当前容量时,需要重新分配更大的内存空间。为了减少内存重新分配的次数,可以选择更合理的增长策略,例如按照当前容量的1.5倍进行扩展,而不是每次都以固定的机制进行扩展,这样可以有效减少内存重新分配的次数,提高效率。 2. 内存占用优化:vector会预留额外的空间,以应对元素的增长,但有时可能会导致内存浪费。可以自定义STL,将内存的分配策略改为按需分配,即根据实际元素数量进行内存的重新分配,避免过度预留内存,减少内存浪费。 3. 迭代器优化:迭代器是vector中用于遍历元素的重要工具,可以通过改进迭代器的实现方式来提高其效率。例如,使用指针实现迭代器,可以直接通过指针的偏移进行元素访问,避免了额外的操作,从而提高了访问速度。 4. 数据访问优化:vector的元素存储是连续的,可以通过优化内存访问的方式提高性能。例如,可以使用缓存行对齐的方式来提高内存访问效率,减少缓存缺失的次数。 5. 多线程支持优化:如果需要在多线程环境下使用自定义的STL vector,可以添加线程安全的机制,避免多线程操作时的竞争条件和数据不一致问题。 总结起来,自定义STL优化vector主要包括动态内存分配优化、内存占用优化、迭代器优化、数据访问优化以及多线程支持等方面,通过对这些方面的改进,可以提高vector的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HySmiley

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值