在c++项目中对于stl的使用检验

本文详细介绍了MFC项目中使用STL(标准模板库)时的多种数据结构,包括vector、map、list、set、hash_map、multiset等的使用条件和区别,并提供了实际操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在MFC项目中使用STL比较多的是:vector,map,list,set,hash_map,mutilSet

说明一下使用的一般条件和相应的区别:

vector:类似数组,可以随意访问任意位置的元素,元素类型没有限制

map:key必须是有序的,在用自定义类型的数据作为key值得时候该类型一定要重载比较运算符,散列数

set:相较于map,不可以直接访问你任意位置的元素,因为他本身就是有序的唯一元素,由于元素都存在内存当中,所以比较耗内存,使用的内存接口是平衡树

hash_map:相较map,存储结构是hash表,数据结构式红黑树,他比map效率更高,但是更耗内存

mutilSet:相较set,他容许元素重复

针对上述class member functions一起还有global functions,需要下载的都在

#include <algorithm>里面有很多和其他成员函数类似功能的函数,但是更加的好用

// testSTL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "StlSample.h"

template<class TYPE>
void printf(vector<TYPE> _Data)
{
	for(vector<TYPE>::iterator it = _Data.begin(); _Data.end() != it; it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}

template<class TYPE,class TYPE1>
void printf(map<TYPE,TYPE1> _Data)
{
	for(map<TYPE,TYPE1>::iterator it = _Data.begin(); _Data.end() != it; it++)
	{
		cout<<"["<<(it->first)<<","<<(it->second)<<"]"<<" "<<endl;
	}
	cout<<endl;
}

template<class TYPE> 
void printf(list<TYPE> _Data)
{
	for(list<TYPE>::iterator it = _Data.begin() ; _Data.end() != it; it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}

template<class TYPE>
void printf(set<TYPE> _Data)
{
	for(set<TYPE>::iterator it = _Data.begin(); _Data.end() != it;it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
}

struct MapStruct
{
	const char* _pData;
	MapStruct(const char* pdata)
	{
		_pData = pdata;
	}

	bool operator < (const MapStruct &obj) const
	{		
		return (strcmp(_pData,obj._pData) <= 0)? true : false;
	}
};

void printf(std::map<const MapStruct,char> _Data)
{
	for(std::map<const MapStruct,char>::iterator it = _Data.begin(); _Data.end() != it; it++)
	{
		cout<<it->first._pData<<"-->"<<it->second<<"   "<<endl;
	}
	cout<<endl;
}

class MyDefSort 
{
public:
	MyDefSort()
	{
		cout<<"MyDefSort()"<<endl;
	}

	MyDefSort(const MyDefSort &obj)
	{
		cout<<"const MyDefSort(const MyDefSort &obj)"<<endl;
	}

private:
	char *m_str;
};

typedef std::map<int, char> MYMAP; 
typedef std::map<const char* ,char> charMAP;
typedef std::map<const MapStruct,char> stuMAP;
int _tmain(int argc, _TCHAR* argv[])
{
	MyDefSort obj;
	MyDefSort obj1;
	obj1 = obj;//构造是要进行内存分配的

#ifdef MYDEFKEY
	stuMAP stuMap1;
	MapStruct s1("1"),s2("2"),s3("3"),s4("4");
 	stuMap1[s1] = '1';
	stuMap1[s2] = '2';
	stuMap1[s3] = '3';
	stuMap1[s4] = '4';
	stuMap1[MapStruct("5")] = '5';

	printf(stuMap1);
#endif
	
#ifdef MAP1
	// Create the first map object.  
	MYMAP charMap1;  

	// Populate the first map with values.  
	charMap1[1] = 'A';  
	charMap1[4] = 'D';  
	charMap1[2] = 'B';  
	charMap1[5] = 'E';  
	charMap1[3] = 'C';  

	// Display the contents of the first map.  
	std::cout << "Contents of first map: " << std::endl;  
	MYMAP::iterator iter;  
	for (iter = charMap1.begin();  
		iter != charMap1.end(); iter++)  
	{  
		std::cout << (*iter).first << " --> ";  
		std::cout << (*iter).second << std::endl;  
	}  
	std::cout << std::endl;  

	// Create the second map object.  
	MYMAP charMap2;  

	// Populate the first map with values.  
	charMap2[1] = 'A';  
	charMap2[4] = 'I';  
	charMap2[2] = 'A';  
	charMap2[5] = 'J';  
	charMap2[3] = 'H';  

	// Display the contents of the second map.  
	std::cout << "Contents of second map: " << std::endl;  
	for (iter = charMap2.begin();  
		iter != charMap2.end(); iter++)  
	{  
		std::cout << (*iter).first << " --> ";  
		std::cout << (*iter).second << std::endl;  
	}  
	std::cout << std::endl;  

	// Compare the maps.  
	if (charMap1 == charMap2)  
		std::cout << "map1 == map2";  
	else if (charMap1 < charMap2)  
		std::cout << "map1 < map2";  
	else if (charMap1 > charMap2)  
		std::cout << "map1 > map2";  
#endif

#ifdef MUTILSET
	// Create the first set object.  
	std::multiset<char> charMultiset1;  

	// Populate the multiset with values.  
	charMultiset1.insert('E');  
	charMultiset1.insert('D');  
	charMultiset1.insert('C');  
	charMultiset1.insert('B');  
	charMultiset1.insert('A');  
	charMultiset1.insert('B');  
	charMultiset1.insert('D');  

	// Display the contents of the first multiset.  
	std::cout << "Contents of first multiset: " << std::endl;  
	std::multiset<char>::iterator iter;  
	for (iter = charMultiset1.begin();  
		iter != charMultiset1.end(); iter++)  
		std::cout << *iter << std::endl;  
	std::cout << std::endl;  

	// Create the second multiset object.  
	std::multiset<char> charMultiset2;  

	// Populate the multiset with values.  
	charMultiset2.insert('J');  
	charMultiset2.insert('I');  
	charMultiset2.insert('H');  
	charMultiset2.insert('G');  
	charMultiset2.insert('F');  
	charMultiset2.insert('G');  
	charMultiset2.insert('I');  

	// Display the contents of the second multiset.  
	std::cout << "Contents of second multiset: "  
		<< std::endl;  
	for (iter = charMultiset2.begin();  
		iter != charMultiset2.end(); iter++)  
		std::cout << *iter << std::endl;  
	std::cout << std::endl;  

	// Compare the sets.  
	if (charMultiset1 == charMultiset2)  
		std::cout << "set1 == set2";  
	else if (charMultiset1 < charMultiset2)  
		std::cout << "set1 < set2";  
	else if (charMultiset1 > charMultiset2)  
		std::cout << "set1 > set2";  
#endif

#ifdef SET
	std::set<char> charSet;

	// Populate the set with values.  
	charSet.insert('E');  
	charSet.insert('D');  
	charSet.insert('C');  
	charSet.insert('B');  
	charSet.insert('A');  

	// Display the contents of the set.  
	std::cout << "Contents of set: " << std::endl;  
	std::set<char>::iterator iter;  
	for (iter = charSet.begin(); iter != charSet.end(); iter++)  
		std::cout << *iter << std::endl;  
	std::cout << std::endl;  

	// Find the D.  
	iter = charSet.find('D');  
	if (iter == charSet.end())  
		std::cout << "Element not found.";  
	else  
		std::cout << "Element found: " << *iter;  
#endif

#ifdef  LIST
	list<int> l1;
	l1.assign(3,2);
	list<int> l2;
	l2.assign(3,3);
	list<int>l3(10,1);
	merge(l1.begin(),l1.end(),l2.begin(),l2.end(),l3.begin());
	printf(l3);//merge 必须是已经排序好的数据 

#endif

#ifdef MAP
#define MAP
	map<int,std::string> m1;
	m1[1] = "n1";
	m1[2] = "n2";
	m1[1] = "n11";
	m1[3] = "n3";
	printf(m1);
#endif

#ifdef CLEAR
	vector<int> v1;
	v1.push_back(1);
	vector<int>::iterator itt = v1.end();
	v1.insert(itt,3,2);
	printf(v1);
	v1.clear();
	printf(v1);
#endif

#ifdef ERASE
	vector<int> v1(1,2);
	vector<int>::iterator it = v1.end();
	v1.insert(it,3);
	it = v1.end();
	v1.insert(it,4);
	printf(v1);
	int &i = v1.front();
	const int &j = v1.front();
	cout<<"i = "<<i<<endl;
	cout<<"j = "<<j<<endl;
	
	int &f = v1.back();
	const int &p = v1.back();
	cout<<"f = "<<f<<endl;
	cout<<"p = "<<p<<endl;

	it = v1.begin();
	v1.erase(it);
	printf(v1);

#endif

#ifdef INSERT
	vector<int> v1,v2,v3;
	v1.push_back(10);
	vector<int>::iterator itt = v1.end();
	v1.insert(itt,20);

	for(vector<int>::iterator it = v1.begin(); v1.end() != it; it++)
	{
		printf("%d ",*it);
	}
	printf("\n");

	v2.assign(v1.begin(), v1.end());
	itt = v2.end();
	v2.insert(itt,7,4);
	
	for(vector<int>::iterator ittt = v2.begin(); v2.end() != ittt; ittt++)
	{
		printf("%d ",*ittt);
	}
	printf("\n");
	
	v3.assign(v1.begin(),v1.end());
	itt = v3.end();
	itt--;
	v3.insert(itt,v2.begin(),v2.end());

	for(vector<int>::iterator it = v3.begin(); v3.end() != it;it++)
	{
		printf("%d ",*it);
	}
	printf("\n");

#endif

#ifdef AT
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);

	for(vector<int>::iterator it = v1.begin(); v1.end() != it; it++)
	{
		printf("%d ",*it);
	}
	printf("\n");
	const int &i = v1.at(0);
	int &j = v1.at(1);
	printf("cosnt i is %d\n",i);
	printf("j is %d\n",j);
#endif



#ifdef ASSIGN
	vector<int> v1,v2,v3;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	for(vector<int>::iterator it= v1.begin(); v1.end() != it;it++)
	{
		printf("%d ",*it);
	}
	printf("\n");

	v2.assign(v1.begin(),v1.end());

	for(vector<int>::iterator it = v2.begin(); v2.end() != it; it++)
	{
		printf("%d ",*it);
	}
	printf("\n");

	v3.assign(7,4);
	for(vector<int>::iterator it = v3.begin(); v3.end() != it; it++)
	{
		printf("%d ",*it);
	}	
	printf("\n");

#endif 

	system("pause");
	return 0;
}


里面针对不同的stl数据结构作出了相应的实例演示


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值