#include #include #include using namespace std; /*** * 实现栈的数据结构并且实现判断符号的对称性{[{[()]}]} * 顺序栈和链式栈 * 后进先出 * 定义pop和push * 支持动态扩容 * 匹配所有string "[{()}]"的关系 ***/ template class StackUse{ public: StackUse(T *array):arr(array){ } T *arr; // 使用指针获得数组,实现动态扩容 void push(T element); // 入栈 T& pop(); // 出栈 int length; // 记录栈的大小 T element; private: StackUse(const StackUse&){} StackUse& operator=(const StackUse&){} int count = 0; // 出栈入栈计数 }; template T& StackUse::pop(){ element = *arr; if(count > 0){ --count; arr--; }else{ return element; } return element; } template void StackUse::push(T element){ if(count < length){ ++arr; // 这里返回的是指针所指向 *arr = element; ++count; cout << *arr << endl; }else{ cout << "out of memory please expend the memory" << endl; } cout << "success!" << endl; } // 判断字符串是否对称 bool synicString(const string& str){ if(str.size() == 0) return false; map signal_count = {{'{','}'},{'[',']'},{'(',')'}}; int str_half_size = str.size()/2; if(str.size()%2 != 0) return false; char ctr[str_half_size]; StackUse ictr(ctr); int i = 0; for(;i icr(ctr); icr.length = 10; // 支持动态扩容 string istr = ""; if(synicString(istr)){ cout << "yes!" << endl; }else{ cout << "Nope!" << endl; } // for(int i = 0; i