实现任意类型序列中元素的反序
- 所涉知识点
- 示例代码
- 开发环境
- 运行结果
- 注意
所涉知识点
阅读此文需要掌握的知识点:回调函数,模板类,类模板,栈。
示例代码
这里直接上代码。
#pragma once
#include <Stack>
using namespace std;typedef void(*PRINTSTACK)(void *);template<typename T>
class ReverseArray
{
public:ReverseArray();~ReverseArray();void pushStack(T data);T getTopStack();void popStack();int getSizeStack();void printStack(void * data, PRINTSTACK print);
private:stack<T> m_stack;
};template<typename T>
inline ReverseArray<T>::ReverseArray()
{
}template<typename T>
inline ReverseArray<T>::~ReverseArray()
{
}template<typename T>
inline void ReverseArray<T>::pushStack(T data)
{m_stack.push(data);
}template<typename T>
inline T ReverseArray<T>::getTopStack()
{return T(m_stack.top());
}template<typename T>
inline void ReverseArray<T>::popStack()
{m_stack.pop();
}template<typename T>
inline int ReverseArray<T>::getSizeStack()
{return m_stack.size();
}template<typename T>
inline void ReverseArray<T>::printStack(void* data,PRINTSTACK print)
{print(data);
}
main.cpp
#include <iostream>
#include <string>
#include "ReverseArray.h"
using namespace std;#define CHAR_SIZE 5
#define STRING_SIZE 7
/*
描述:反转数组(任意类型)
*/typedef struct PERSON
{int age;char name[64];
}Person;void myPrint(void *data)
{Person *p = (Person*)data;cout << "age;" << p->age << " name:" << p->name << endl;
}void printString(void * data)
{string str = *((string*)data);cout << str << endl;
}void printChar(void * data)
{char c = *((char*)data);cout << c << endl;
}void test()
{ReverseArray<Person> *pStack = new ReverseArray<Person>;Person p1 = { 3,"hudf"};Person p2 = { 5,"akso"};Person p3 = { 7,"及家属的" };Person p4 = { 8,"口袋"};Person p5 = { 8,"husdh"};pStack->pushStack(p1);pStack->pushStack(p2);pStack->pushStack(p3);pStack->pushStack(p4);pStack->pushStack(p5);while (pStack->getSizeStack() > 0){Person data = pStack->getTopStack();pStack->printStack((void *)&data,myPrint);pStack->popStack();//进出栈操作的是栈顶}cout << "========string array============" << endl;ReverseArray<string> *pStringStack = new ReverseArray<string>;string array[] = { "tyu","hello","start","wei","come","waht","world"};string newArray[STRING_SIZE];
// cout << array->size() << endl;//这里取第一个字符串中元素的个数for (size_t i = 0; i < STRING_SIZE; ++i){pStringStack->pushStack(array[i]);}cout << "stack size:" << pStringStack->getSizeStack() << endl;int i = 0;while (pStringStack->getSizeStack() > 0){string data = pStringStack->getTopStack();pStringStack->printStack((void *)&data, printString);newArray[i] = data;pStringStack->popStack();//进出栈操作的是栈顶++i;}cout << "=============print newArray============" << endl;for (int i = 0; i < STRING_SIZE; ++i){cout << newArray[i]<< " ";}cout << endl;cout << "========char array============" << endl;ReverseArray<char> *pCharStack = new ReverseArray<char>;char charArray[] = {'a','b','c','d','e'};char newCharArray[CHAR_SIZE];for (size_t i = 0; i < CHAR_SIZE; ++i){pCharStack->pushStack(charArray[i]);}cout << "stack size:" << pCharStack->getSizeStack() << endl;int n = 0;while (pCharStack->getSizeStack() > 0){char data = pCharStack->getTopStack();pCharStack->printStack((void *)&data, printChar);newCharArray[n] = data;pCharStack->popStack();//进出栈操作的是栈顶++n;}cout << "=============print newCharArray============" << endl;for (int i = 0; i < CHAR_SIZE; ++i){cout << newCharArray[i] << " ";}cout << endl;}
int main()
{test();return 0;
}// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
开发环境
vs2017控制台输出程序。
运行结果
注意
类模板的使用需要将类的声明与定义放在.h文件中,因为模板本身是一种不确定的类型,编译器在编译的时候需要在.h文件中去找模板中相应的定义。