RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C++STLstack括号匹配源代码
// STL_stack.cpp : 定义控制台应用程序的入口点。
// STL 栈stack

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;

multimap match;
multimap trueMatch;
typedef pair::iterator, multimap::iterator> pairItr;
stack st;
void initMatch(){
    /*
    \ ( [ { } ] )
    ( 2 2 2 0 0 1
    [ 2 2 2 0 1 0
    { 2 2 2 1 0 0
    } 0 0 1 0 0 0
    ] 0 1 0 0 0 0
    ) 1 0 0 0 0 0
    */

    match.insert(make_pair('(', ')'));
    match.insert(make_pair('(', '['));
    match.insert(make_pair('(', '{'));
    match.insert(make_pair('(', '('));

    match.insert(make_pair('[', ']'));
    match.insert(make_pair('[', '('));
    match.insert(make_pair('[', '['));
    match.insert(make_pair('[', '{'));

    match.insert(make_pair('{', '}'));
    match.insert(make_pair('{', '{'));
    match.insert(make_pair('{', '['));
    match.insert(make_pair('{', '('));

    match.insert(make_pair(')', '('));
    match.insert(make_pair(']', '['));
    match.insert(make_pair('}', '{'));
    /*---------------------------*/
    trueMatch.insert(make_pair('(', ')'));
    trueMatch.insert(make_pair(')', '('));
    trueMatch.insert(make_pair(']', '['));
    trueMatch.insert(make_pair('[', ']'));
    trueMatch.insert(make_pair('{', '}'));
    trueMatch.insert(make_pair('}', '{'));
}

bool isMatch(char ch){

    if (st.empty()){
        st.push(ch);
        return true;
    }

    pairItr pair = match.equal_range(st.top());
    bool isExit = true;
    for (auto itr = pair.first; itr != pair.second;itr++)
    {
        if ((*itr).second == ch){
            isExit = false;
            break;
        }
    }
    if (isExit){
        return false;
    }

    if(trueMatch.find(st.top()) != trueMatch.end() && (*trueMatch.find(st.top())).second == ch){
        st.pop();
        return true;
    }
    st.push(ch);
    return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
    clock_t begin_clock_t = clock();
    initMatch();
    //最糟糕情况 匹配情况  最后一位不匹配
    //string str = "([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])[";
     string str = "([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])";

    for (auto var = str.begin(); var != str.end();var++)
    {
        if(!isMatch(*var))break;
    }
    if (st.empty()){
        cout << "----------匹配-----------" << endl;
    }
    else{
        cout << "不匹配" << endl;
    }
    clock_t end_clock_t = clock();
    cout << double(end_clock_t - begin_clock_t) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
    getchar();
    return 0;
}

本文题目:C++STLstack括号匹配源代码
文章路径:http://scyingshan.cn/article/joosoj.html