STL顺序容器简单应用

算法迭代器入门

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

void main01()
{
	vector<int> v1;//1容器 把你的元素拷贝进容器
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	//2迭代器 迭代器相当于指针
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}

	//算法和迭代器能无缝连接
	int num1 = count(v1.begin(), v1.end(), 3);
	cout << num1 << endl;
}


//容器装元素
class Teacher
{
public:
	int age;
	char name[60];
};

void main02()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	vector<Teacher>v1;
	v1.push_back(t1);
	v1.push_back(t2);
	v1.push_back(t3);

	for (vector<Teacher>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << it->age << " ";
	}
}

//容器装指针
void main03()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	Teacher *p1, *p2, *p3;
	p1 = &t1;
	p2 = &t2;
	p3 = &t3;

	vector<Teacher*>v1;
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);

	for (vector<Teacher*>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << (*it)->age << " " << endl;
	}
}

void main()
{
	main03();
}

string

#define _SCL_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

void main21()
{
	string s1 = "aaaa";
	string s2("bbbbb");
	string s3 = s2;
	string s4(10, 'a');
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;

}

void main22()
{
	string s1 = "asdf";
	try
	{
		for (int i = 0; i < s1.length(); i++)
		{
			cout << s1[i] << " ";//[]不会抛异常
		}
	}
	catch (...)
	{
		cout << "发生异常1!" << endl;
	}
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}

	try
	{
		for (int i = 0; i < s1.length(); i++)
		{
			cout << s1.at(i) << " ";//at可以抛异常
		}
	}
	catch (...)
	{
		cout << "发生异常2!" << endl;
	}
	
}

//字符指针的string的转换
void main23()
{
	string s1 = "aaaasdf";//char*===>string

	//s1===>char *
	cout << s1.c_str() << endl;

	//s1 的内容copy 到buf中
	char buf1[20] = {0};
	s1.copy(buf1, 3,0);//只给你copy3个字符,不会变成C风格的字符串。从0的位置开始拷贝
	cout << buf1 << endl;

}

//字符串的连接
void main24()
{
	string s1 = "aaa";
	string s2 = "bbb";
	s1 = s1 + s2;
	cout << s1 << endl;
	string s3 = "3333";
	string s4 = "4444";
	s3.append(s4);
	cout << s3 << endl;
}

//字符串的替换和查找
void main25()
{
	string s1 = "bmw 111 bmw 222 bmw 333 bmw 555";
	int index = s1.find("bmw", 0);
	cout << index << endl;

	//案例1 求bmw出现的次数 每一次出现的数组下标
	int offindex = s1.find("bmw", 0);
	while (offindex != string::npos)
	{
		cout << "index:" << offindex << endl;
		offindex += 1;
		offindex = s1.find("bmw", offindex);
	}

	int offindex2 = s1.find("bmw", 0);
	while (offindex2 != string::npos)
	{
		s1.replace(offindex2, 1, "BMWW");//案例2 从offindex2位置开始替换1个字符,替换为“BMW”
		offindex2 += 1;
		offindex2 = s1.find("bmw", offindex2);
	}
	cout << s1 << endl;

}

//删除字符
void main26()
{
	string s1 = "hello1 hello2 world";
	string::iterator it = find(s1.begin(), s1.end(), 'h');
	if (it != s1.end())
	{
		s1.erase(it);
	}
	cout << s1 << endl;

	s1.erase(s1.begin(), s1.end());//全部删除
	cout << s1 << s1.length() << endl;

	s1.insert(0, "AAA");//在头部插入AAA
	s1.insert(s1.length(), "CCC");//在尾部插入CCC
	cout << s1 << endl;
}

//转换大小写
void main27()
{
	string s1 = "AAAbbb";
	string s2 = "aaaBBB";

	//transform后面可以添加
	transform(s1.begin(), s1.end(), s1.begin(), toupper);
	transform(s2.begin(), s2.end(), s2.begin(), tolower);
	cout << s1 << endl;
	cout << s2 << endl;
}

void main()
{
	main27();
}


vector

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

void main31()
{
	vector<int> v1;
	cout << v1.size() << endl;

	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);

	cout << v1.size() << endl;
	cout << v1.front() << endl;   //获取头部元素

	//修改头部元素的值
	v1.front() = 11;   //说明front()返回值可以当左值
	cout << v1.front() << endl;

	//while (v1.size() > 0)
	//{
	//	cout << v1.back() << endl;//获取尾部元素
	//	v1.pop_back();			  //删除尾部元素
	//}
}

//vector初始化
void main32()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	v1.push_back(7);

	vector<int> v2(v1);//或者 v2 = v1
	vector<int> v3(v1.begin(), v1.begin() + 2);

}

void printV(vector<int> v)
{
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
}


//vector的遍历
void main33()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		//v1[i] = i;//还没有分配内存,是会出问题的!!
		v1.push_back(i);
	}

	//for (int i = 0; i < 10; i++)
	//{
	//	cout << v1[i] << " ";//当i超出已分配内存时,也会宕机哟!!
	//}
	printV(v1);
}

void main34()
{
	vector<int> v1(10);//会为已分配的内存 填充 0
	v1.push_back(100);
	cout << v1.size() << endl;
	printV(v1);
}

//迭代器
//	1			3			5
//begin()				      end()
//当it==v1.end()的时候 说明这个容器已经遍历完毕

//迭代器的种类
//typedef typename _Mybase::iterator iterator;
//typedef typename _Mybase::const_iterator const_iterator;//只读迭代器
//
//typedef _STD reverse_iterator<iterator> reverse_iterator;//反向迭代器
//typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;

void main35()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1);
	}

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}

	//vector的逆向遍历
	for (vector<int>::reverse_iterator rit = v1.rbegin(); rit != v1.rend(); rit++)
	{
		cout << *rit << " ";
	}
}


//vector的删除
void main36()
{
	vector<int> v1(10);
	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = i + 1;
	}

	//区间删除
	v1.erase(v1.begin(), v1.begin() + 3);
	printV(v1);

	//指定位置删除
	v1.erase(v1.begin() + 1);
	cout << endl;
	printV(v1);

	//指定值删除
	v1[0] = 100;
	v1[5] = 100;
	cout << endl;
	printV(v1);
	for (vector<int>::iterator it = v1.begin(); it != v1.end();)
	{
		if (*it == 100)
		{
			it = v1.erase(it);
		}
		else
		{
			it++;
		}
	}
	cout << endl;
	printV(v1);
}

void main()
{
	main36();

}


deque双向容器

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;

void printD(deque<int> d)
{
	for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
}

void main41()
{
	deque<int> d1;
	d1.push_back(1);
	d1.push_back(3);
	d1.push_back(5);

	d1.push_front(-11);
	d1.push_front(-33);
	d1.push_front(-55);

	printD(d1);
	cout << endl;

	d1.pop_back();
	d1.pop_front();
	printD(d1);
	cout << endl;

	//获取-33的下标
	deque<int>::iterator it = find(d1.begin(), d1.end(), -33);
	if (it != d1.end())
	{
		cout << "-33的下标是:" << distance(d1.begin(), it) << endl;
	}
	else
		cout << "-33不存在!" << endl;
}

void main()
{
	main41();
}


stack

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

//先进后出
void main51()
{
	stack<int> s;

	//入栈
	for (int i = 0; i < 10; i++)
	{
		s.push(i + 1);
	}

	cout << "栈的大小为:" << s.size() << endl;

	//出栈
	while (!s.empty())
	{
		int tmp = s.top();
		cout << tmp << " ";
		s.pop();
	}
}

class Teacher
{
public:
	int age;
	char name[32];
public:
	void printT()
	{
		cout << "age:" << age << endl;
	}

};

void main52()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	stack<Teacher> s;
	s.push(t1);
	s.push(t2);
	s.push(t3);

	while (!s.empty())
	{
		Teacher tem = s.top();
		tem.printT();
		s.pop();
	}
}

void main53()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	stack<Teacher*> s;

	//入栈
	s.push(&t1);
	s.push(&t2);
	s.push(&t3);

	//出栈
	while (!s.empty())
	{
		Teacher *tmp = s.top();
		tmp->printT();
		s.pop();
	}
}



void main()
{
	main53();

}


queue

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

//queue先进先出
void main61()
{
	queue<int> q;
	q.push(1);
	q.push(3);
	q.push(5);

	cout << "队首元素:" << q.front() << endl;
	cout << "队尾元素:" << q.back() << endl;

	while (!q.empty())
	{
		int tmp = q.front();
		cout << tmp << endl;
		q.pop();
	}
}

class Teacher
{
public:
	int age;
	char name[32];
public:
	void printT()
	{
		cout << "age:" << age << endl;
	}

};

void main62()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	queue<Teacher> q;
	q.push(t1);
	q.push(t2);
	q.push(t3);

	while (!q.empty())
	{
		Teacher tmp = q.front();
		tmp.printT();
		q.pop();
	}
}

void main63()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	queue<Teacher*> q;
	q.push(&t1);
	q.push(&t2);
	q.push(&t3);

	while (!q.empty())
	{
		Teacher *tmp = q.front();
		tmp->printT();
		q.pop();
	}
}

void main()
{
	main63();
}


list

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

void main71()
{
	list<int> l;
	cout << "插入之前的大小" << l.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i + 1);//尾插法
	}
	cout << "插入之后的大小" << l.size() << endl;

	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//list不能随机访问
	it = l.begin();
	it++;
	it++;
	it++;
	//it = it + 5;//不支持随机访问容器
	l.insert(it, 100);
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
}
//结论:链表的节点index序号是从0号位置开始
//		在3号位置插入元素,让原来的3号位置编程4号位置,4号位置变为5号位置


void main72()
{
	list<int> l;
	cout << "插入之前的大小" << l.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i + 1);//尾插法
	}
	cout << "插入之后的大小" << l.size() << endl;

	list<int>::iterator it1 = l.begin();
	list<int>::iterator it2 = l.begin();
	it2++;
	it2++;
	it2++;
	
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	l.erase(it1, it2);//左闭右开

	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}

	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);

	cout << endl;
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}
	l.remove(100); 
	cout << endl;
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}

}


void main()
{
	main72();
}


priority_queue
#include<iostream>
#include<queue>
#include<functional>
using namespace std;

void main81()
{
	priority_queue<int> p1;//默认的是最小值优先级,即最小值在队首
	priority_queue<int, vector<int>, less<int>> p2;
	priority_queue<int, vector<int>, greater<int>> p3;//最大值优先级

	p1.push(11);
	p1.push(55);
	p1.push(22);
	p1.push(33);

	cout << "p1的队首元素为:" << p1.top() << endl;
	cout << "p1的大小为:" << p1.size() << endl;

	//出队
	while (p1.size() > 0)
	{
		cout << p1.top() << " ";
		p1.pop();
	}

	p3.push(11);
	p3.push(55);
	p3.push(22);
	p3.push(33);

	cout << "p3的队首元素为:" << p3.top() << endl;
	cout << "p3的大小为:" << p3.size() << endl;

	//出队
	while (p3.size() > 0)
	{
		cout << p3.top() << " ";
		p3.pop();
	}


}

void main()
{
	main81();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值