<1>stack
#include <iostream>
#include <stack>
using namespace std;
void test01()
{
//构造
stack<int> s;
// 入栈
s.push(10);
s.push(20);
s.push(30);
// 拷贝构造
stack<int> s1(s);
cout << "s1的栈顶元素是:" << s1.top() << endl;
cout << "s1的大小是:" << s1.size() << endl;
cout << "s1是否为空: " << s1.empty() << endl;
// 出栈
s1.pop();
s1.pop();
s1.pop();
cout << "s1是否为空: " << s1.empty() << endl;
// 赋值操作
stack<int> s2 = s;
cout << "s2的大小:" << s2.size() << endl;
}
// 对栈中的元素进行遍历输出
// 定义函数直接输出栈中元素
template <class T>
void printStack(stack<T>& stk)
{
while (!stk.empty())// 只要stack容器不为空,就继续输出,如果为空就终止循环
{
cout << stk.top() << " ";
stk.pop(); // 弹出栈顶元素
}
cout << endl;
}
void test02()
{
stack<string> stk1;
stk1.push("abc");
stk1.push("bcd");
stk1.push("cbd");
printStack(stk1);
stack<int> stk2;
stk2.push(30);
stk2.push(20);
stk2.push(10);
printStack(stk2);
}
// 练习1:给定一个由"("和")"组成的字符串,判断左右括号是否成对出现
// 成对情况:(abc(aac))
// 不成对的情况:)(abc)( (abc)) ((abc)
// 如果遇到了左括号就直接入栈,如果遇见右括号,判断栈里面是否有左括号,有就直接弹出,没有就是不匹配, 如果右括号检查完毕都有和他匹配的左括号,最后还要再检查栈中是否为空,如果不为空说明有多余的左括号,如果为空就说明完全匹配。
bool isMatch(string str)
{
stack<char> stk; // 准备一个栈来存放左括号
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '(') // 如果先遇到做括号就直接入栈
{
stk.push('(');
}
else if (str[i] == ')') // 如果遇到了右括号
{
if (stk.empty()) // 如果为空就说明栈中没有左括号,就直接终止函数返回false
{
return false;
}
else
{
stk.pop(); // 如果不为空就直接弹出一个左括号,一对括号就匹配完成了
}
}
}
// 遍历结束之后,也就是所有右括号都有匹配的左括号
// 还要再次检查栈容器中是否还有多余的左括号,如果有说明没有完全匹配,要返回false
// 如果栈为空,就说明所有的左括号都和右括号匹配完毕了,返回true即可
if (stk.empty())
{
return true;
}
else
{
return false;
}
}
void test03()
{
string s1 = "(ab(c))";
string s2 = ")(ab(c))";
string s3 = "((ab(c))";
string s4 = ")(ab(c))(";
string s5 = "(ab(c)))";
cout << isMatch(s1) << endl;
cout << isMatch(s2) << endl;
cout << isMatch(s3) << endl;
cout << isMatch(s4) << endl;
cout << isMatch(s5) << endl;
}
int main()
{
test03();
return 0;
}
<2>queue
#include <iostream>
#include <queue>
using namespace std;
// 定义一个打印队列中所有元素的函数
template <class T>
void printQueue(queue<T>& que)
{
while (!que.empty())
{
cout << que.front() << " ";
que.pop();
}
cout << endl;
}
void test01()
{
queue<int> que;
for (int i = 1; i < 11; i++)
{
que.push(i);
}
cout << "队头:" << que.front() << endl;
cout << "队尾:" << que.back() << endl;
que.pop();
cout << "队头:" << que.front() << endl;
cout << "队尾:" << que.back() << endl;
cout << que.size() << endl;
cout << que.empty() << endl;
printQueue(que);
}
// 练习1:在queue容器中存放Person类型的对象(成员包括mName和mAge),并对对象的成员进行遍历输出
class Person
{
public:
string mName;
int mAge;
Person(string name, int age) :mName(name), mAge(age) {}
};
void test02()
{
queue<Person> q;
// 准备对象
Person p1("唐僧", 500);
Person p2("孙悟空", 1000);
Person p3("二郎神", 2000);
q.push(p1);
q.push(p2);
q.push(p3);
while (!q.empty())
{
cout << "姓名是:" << q.front().mName << "年龄是:" << q.front().mAge << endl;
q.pop();
}
}
// 练习2:奇数和偶数的分类
// 在queue容器中存放一些整数,要求把这些整数进行分类,奇数放到一组queue容器中,偶数放到另外一组容器中。
// 分类完成后在函数中进行打印输出即可
void numType(queue<int>& que)
{
queue<int> q1; // 存放奇数
queue<int> q2; // 存放偶数
while (!que.empty())
{
if (que.front() % 2 != 0) // 奇数
{
q1.push(que.front()); // 进q1容器
}
else
{
q2.push(que.front()); // 偶数进q2容器
}
que.pop(); // 处理完的数据将其弹出即可
}
printQueue(q1);
printQueue(q2);
}
int main()
{
queue<int> q;
q.push(5);
q.push(2);
q.push(6);
q.push(1);
q.push(3);
numType(q);
return 0;
}
<3>list
#include <iostream>
#include <list>
using namespace std;
//定义打印list的函数
template <class T>
void printList(list<T>& ls)
{
// 如果要在for循环中使用迭代器进行泛型的使用,需要加typename关键字。
for (typename list<T>::iterator it = ls.begin(); it != ls.end(); it++)
{
cout << *it << " ";
}
cout << endl;
C++11推荐的方式
auto代表自动推导数据类型, ele是自定义的变量名, ls是传进来的容器
//for (auto ele : ls)
//{
// cout << &ele << " ";
//}
//cout << endl;
}
void test01()
{
list<int> L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
L1.push_front(100);
L1.push_front(200);
L1.push_front(300);
L1.push_front(400);
printList(L1);
list<int> L2(L1.begin(), L1.end());
printList(L2);
list<int> L3(L2);
printList(L3);
list<int> L4(10,300);
printList(L4);
}
void test02()
{
list<int> L1 = { 10,20,30,40 };
list<int> L2 = L1;
printList(L2);
list<int> L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int> L4;
L4.assign(5, 500);
printList(L4);
L1.swap(L4);
printList(L1);
}
void test03()
{
list<int> L1 = { 10,20,30,40,50 };
cout << L1.size() << endl;
cout << L1.empty() << endl;
// 重新指定容器中的元素个数
L1.resize(3);
printList(L1);
L1.resize(10, 100);
printList(L1);
}
// 插入删除和获取元素
void test04()
{
list<int> L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_front(100);
L1.push_front(200);
L1.push_front(300);
L1.pop_back();
L1.pop_front();
printList(L1);
L1.insert(++++L1.begin(), 500);
printList(L1);
L1.erase(++L1.begin());
printList(L1);
L1.insert(L1.begin(), 3, 1000);
printList(L1);
list<int> L2 = { 10000,20000,30000 };
L1.insert(L1.begin(), L2.begin(), L2.end());
printList(L1);
L1.remove(1000);
printList(L1);
cout << L1.front() << endl;
cout << L1.back() << endl;
L1.clear();
cout << L1.size() << endl;
}
// 处理list迭代器失效的问题
// 删除会导致当前的迭代器失效,需要更新迭代器
void test05()
{
list<int> ls = { 5,2,8,2 };
for (list<int>::iterator it = ls.begin(); it != ls.end();)
{
if (*it == 2)
{
it = ls.erase(it);
}
else
{
it++;
}
}
printList(ls);
}
// 插入元素并不会导致迭代器失效
void test06()
{
list<int> ls = { 5,2,2,8,2 };
for (list<int>::iterator it = ls.begin(); it != ls.end(); it++)
{
if (*it == 2)
{
ls.insert(it, 100);
}
}
printList(ls);
}
bool myCompare(int a, int b)
{
return a > b;
}
void test07()
{
list<int> L1 = { 30,10,40,50,1,2,5 };
L1.reverse();
printList(L1);
// 默认是按照升序排序
L1.sort();
printList(L1);
// 指定降序排序list容器
L1.sort(myCompare);
printList(L1);
}
class Person
{
public:
string mName;
int mAge;
Person(string name, int age) :mName(name), mAge(age) {}
};
// 规则:按照person对象的年龄进行升序排序,如果年龄相同,就按照名字的降序排序
bool comPerson(Person& p1, Person& p2)
{
if (p1.mAge == p2.mAge) // 如果年龄相同按照姓名降序排
{
return p1.mName > p2.mName;
}
else // 如果年龄不同按照年龄升序排
{
return p1.mAge < p2.mAge;
}
}
void test08()
{
Person p1("jieke", 1000);
Person p2("chudai", 2000);
Person p3("zuofei", 2000);
Person p4("saiwen", 1000);
list<Person> ls = { p1,p2,p3,p4 };
ls.sort(comPerson);
for (auto ele : ls)
{
cout << ele.mName << "\t" << ele.mAge << endl;
}
}
// 编写代码实现将list链表容器中的一系列手机号中不合法的给删除掉,只保留合法的手机号,将结果进行输出
// 合法规则:11位数字并且要以1开头
//
int main()
{
test08();
return 0;
}