一个括号匹配方法,用到数据结构栈
#include#include #include #include using namespace std; string::iterator it; stack st; bool valid_braces(string braces) { for (it = braces.begin(); it != braces.end(); it++) { if (*it == '[' || *it == '(' || *it == '{') { st.push(*it); } if (*it == ']') { if (st.top() == '[') { st.pop(); } else { st.push(*it); } } if (*it == '}') { if (st.top() == '{') { st.pop(); } else { st.push(*it); } } if (*it == ')') { if (st.top() == '(') { st.pop(); } else { st.push(*it); } } } if (st.empty()) { return(1); } else { return(0);



