C++STL容器

定长数组

array

#include <iostream>
#include <array>//包含定长数组的头文件

using namespace std;

template <class T,size_t size>
class myarray
{
public:
	myarray();
	myarray(T memory, int curSize) :memory(memory), curSize(curSize) {}
protected:
	T memory;
	int curSize;
};
void testarray()
{
	myarray<int, 3> array1D;
}//仿造array

int main()
{
	array<int, 3> array1D;
	for (int i = 0; i < array1D.size(); i++)
	{
		array1D[i] = i;
	}
	array<string, 3>* p = new array<string, 3>;
	delete p;
	p = nullptr;

	array<int, 3> num = { 8,8,8 };
	cout << num.empty() << endl;//判断是否为空
	num.fill(3);//把所有的元素都填充为3
	for (auto V : num)
	{
		cout << V << endl;
	}
	array <int, 3> num1 = { 9,9,9 };
	num.swap(num1);
	return 0;
}

动态数组

vector

#include <vector>
#include <iostream>
#include <string>
using namespace std;

template <class t>
void printVector(vector<t>& temp)
{
	for (auto v : temp)
	{
		cout << v << endl;
	}
}
void testVector()
{
	//模板类型:存储的数据类型
	vector<int> vecData;//不带长度
	for (int i = 0; i < 3; i++)
	{
		vecData.push_back(i);//尾插法
	}
	vector<string> strData(3);//当前动态数组的长度为3
	for (int i = 0; i < 3; i++)
	{
		string name = "name";
		strData[i] = name + to_string(i);//在确定长度以内的可以用数组法插入
	}
	vector<double> DDAta = { 8,8,8,8 };//带初始化
	printVector(DDAta);
}

int main()
{
	vector<int> iData = { 9,9,9,9,9,9 };
	iData.size();//当前容器中元素的个数
	iData.empty();//判断当前容器是否为空
	iData.front();//访问第一个元素
	iData.back();//访问最后一个元素
	iData.at(3);//下标方式访问
	iData.emplace(iData.begin() + 2, 100);//修改下标是2的元素为100
	iData.emplace_back(99);//在最后插入元素为99
	return 0;
}

容器之间都是可以嵌套的

#include <iostream>
#include <array>
#include <vector>
#include <string>

using namespace std;
//嵌套
void testArray()
{
	array<array<int, 3>, 4> arrData;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			arrData[i][j] = i * j;
		}
	}
}
void testVector()
{
	vector<vector<int>> vecData;
	for (int i = 0; i < 4; i++)
	{
		vector<int> temp;
		for (int j = 0; j < 3; j++)
		{
			temp.push_back(i * j);
		}
		vecData.push_back(temp);
	}
	//不等列数的动态数组
	for (int i = 0; i < vecData.size(); i++)
	{
		for (int j = 0; j < vecData[i].size(); j++)
		{
			cout << vecData[i][j] << endl;
		}

	}
}
void tetsArrayVector()
{
	array<vector<int>, 3> vecData;
	vector<int> vec[3] = { {8,8,8,8},{9,9,9,9,9},{6,6,6,6} };
	for (int i = 0; i < vecData.size(); i++)
	{
		vecData[i] = vec[i];
	}
}

双向链表

list

#include <iostream>
#include <list>
using namespace std;

void tetsList()
{
	list<int> iNum;
	list<string> strNum;
	strNum.push_back("尾插法插入1");
	list<string>::iterator iter;//不删除的方式遍历
	for (iter = strNum.begin(); iter != strNum.end(); iter++)
	{
		cout << *iter << endl;
	}
	while (!strNum.empty())//删除方式遍历
	{
		cout << strNum.front() << "";
		strNum.pop_front();//删除头部
	}
	//find算法
	for (int i = 0; i < 3; i++)
	{
		iNum.push_back(i);
	}
	auto result = find(iNum.begin(), iNum.end(), 2);//在区间内找2这个整数
	//其他操作
	iNum.reverse();//反转
	iNum.sort();//排序
	//更多可以查阅官方文档
}
class student
{
public:
	student(string name, int age, int scor) :name(name), age(age), scor(scor) {}
	void print()
	{
		cout << name << endl << age << endl << scor;
	}
protected:
	string name;
	int age;
	int scor;
};
void testUserdata()
{
	list<student> Sdata;
	string name;
	int age;
	int scor;
	while (1)
	{
		cout << "input Sdata" << endl;
		cin >> name >> age >> scor;
		Sdata.push_back(student(name, age, scor));
		cout << "是否继续?";
		while (cin.get() != 'n');
		if (cin.get() == 'n')
			break;
	}
	for (auto v : Sdata)
	{
		v.print();
	}
}

stack

#include <iostream>
#include <string>
#include <stack>
using namespace std;

void testStack()
{
	//穿脱原则,先进后出
	stack<int> intStack;
	for (int i = 0; i < 3; i++)
	{
		intStack.push(i);//入栈
	}//入栈顺序0,1,2
	while (!intStack.empty())
	{
		cout << intStack.top() << " ";//输出栈顶元素
		intStack.pop();//删除
	}//出栈顺序2,1,0
}
void binary(int num)//二进制转化
{
	stack<int> bin;
	while (num)
	{
		bin.push(num % 2);
		num = num / 2;
	}
	if (bin.size() < 8)//如果不足八位就补零
	{
		for (int i = bin.size(); i <= 8; i++)
		{
			bin.push(0);
		}
	}
	while (!bin.empty())
	{
		cout << bin.top();
		bin.pop();
	}
}

队列

queue、deque,priority_queue      普通对列,双向队列,优先队列

#include <iostream>
#include <queue>//普通队列头文件
#include <deque>//双向队列头文件
#include <concurrent_priority_queue.h>//优先队列头文件
#include <string>
using namespace std;
void testQueue()//普通队列
{
	queue<int> qData;//队列与栈相对应,数据先入者先出
	for (int i = 0; i < 3; i++)
	{
		qData.push(i);//入队
	}//入队0,1,2
	while (!qData.empty())
	{
		cout << qData.front() << " ";//获取队头
		qData.pop();//出队,个人理解就是删除
	}//出队0,1,2
}
void testDeque()//双向队列
{
	deque<int> dData;
	for (int i = 0; i < 3; i++)
	{//入队的两种方式
		dData.push_back(i);//尾插法入队
		dData.push_front(i);//头插法入队
	}
	while (!dData.empty())
	{
		cout << dData.front();//从头部出队
		dData.pop_front();
	}
	while (!dData.empty())
	{
		cout << dData.back();//从尾部出队
		dData.pop_back();
	}
}

集合

set、multiset、bitset

#include <iostream>
#include <ctime>
#include <bitset>//专门处理二进制
#include <set>//集合头文件
using namespace std;
void testbit()
{
	bitset<8> bData(11110000);//<>中表示有多少个位
	bData.flip();//反转,官方文档有个更多函数;
}
//集合set  数据自带排序  数据的唯一性(即重复的数据会删除一个)
void testmultiset()//多重集合,只具有排序功能,不具有去除功能
{
	multiset<int> mData;
	for (int i = 0; i < 10; i++)
	{
		mData.insert(rand() % 10);
	}
	for (auto v : mData)
	{
		cout << v << endl;
	}
}
int main()
{
	srand((unsigned int)time(nullptr));//随机数种子
	set<int> setData;
	for (int i = 0; i < 10; i++)
	{
		setData.insert(rand() % 10);//在集合中随机插入10个数
	}
	for (set<int>::iterator iter = setData.begin(); iter != setData.end(); iter++)
	{
		cout << *iter << " ";
	}
	return 0;
}

映射

map、multiset

#include <iostream>
#include <map>
using namespace std;
//template <class t1,class t2>
//struct MyPair
//{
//	t1 one;
//	t2 two;
//};
void testpair()
{
	pair<int, string> pData(1, "string");
	cout << pData.first << " ";
}
//map
//自带排序默认是从小到大,数据唯一性
void testmap()
{
	map<int, string> maData;
	//几种插入方式
	maData.insert(pair<int, string>(1, "string"));
	maData.insert(make_pair<int, string>(2, "string"));
	maData[-1] = string("string");
	//遍历
	for (auto v : maData)
	{
		cout << v.first << " " << v.second << endl;
	}
}
void testmultimap()
{
	//多重映射,没有限制,什么对应关系都能插入,不能用下标法
	multimap<int, string> mulData;
	mulData.insert(pair<int, string>(1, "string"));
}

列表

initializer_list

元组

tupe

#include <tuple>
#include <iostream>
using namespace std;
void testTuple()
{//元组,把任何类型的一系列数据当作一组处理
	tuple<string, int, int, string> tData = { "string",8,8,"string" };
	tuple<double, int, string> aray[3];
}
void visitData()
{
	tuple<string, int, int, string> mdata = { "string",8,8,"string" };
	//get的方法
	cout << get<0>(mdata) << endl;//获取第1个元素,,,,这个不能用for循环
	//tie的方法
	string name;
	int age;
	int num;
	string tel;
	tie(name, age, num, tel) = mdata;
	cout << name << " " << num << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值