STL 笔记

map<const string,int> wordmap;
bool NotNeed(char c)
{
    return ispunct(c);
}
bool NotDigtilAndNotDouble(const std::pair<const string, int>& word)
{
    const char* p = word.first.c_str();
    while(*p!='\0')
    {
        if (*p >='0' && *p <='9')
        {
            return true;
        }
        p++;
    }
    if (word.second>1)
    {
        return true;
    }
    return false;
}
vector< pair<string, int> > vecWords;
list<pair<string, int> > lstWords;

template <typename T1,typename T2 = int>
struct comp
{
    comp(const T1& s): _s(s){}

    bool operator() (const std::pair< T1, T2>& p)
    {
        return (p.first == _s);
    }
    T1 _s;
};

struct NoMore
{
    bool operator()(const std::pair<std::string, int>&p)
    {
        if (p.second>1)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }
};

bool IsDelete(const std::pair<std::string, int>&p)
{
    return p.second>1;
}

bool is_digits(const std::pair<std::string, int>&p)
{
    return p.first.find_first_not_of("0123456789") == std::string::npos;
}

void main()
{
    char* str = "start program this is thread id = 2323, this is my first test (error code = 999) kjhkjh 2000-01-01 00:00:00";
    char* expert = "start program this is thread id = %d, this is my first test (error code = %d) kjhkjh";
    string s1 = str;
    s1.erase(remove_if(s1.begin(),s1.end(),NotNeed),s1.end());
    string s2 = str;
    replace_if(s2.begin(),s2.end(),NotNeed,' ');
    string word;
    stringstream strstream(s2);
    while(strstream>>word)
    {
        wordmap[ word ]++;
        vector< pair<string, int> >::iterator it = find_if(vecWords.begin(),vecWords.end(),comp<string>(word));
        if (it != vecWords.end())
        {
            it->second++;
        }
        else
        {
            vecWords.push_back(std::make_pair(word,1));
        }

        list<pair<string, int> >::iterator lstIt = find_if(lstWords.begin(),lstWords.end(),comp<string>(word));
        if (lstIt != lstWords.end())
        {
            lstIt->second++;
        }
        else
        {
            lstWords.push_back(std::make_pair(word,1));
        }

    }
    vector< pair<string, int> >::iterator it = vecWords.begin();
    vecWords.erase(remove_if(vecWords.begin(),vecWords.end(),NoMore()));
    lstWords.remove_if(IsDelete);
    lstWords.remove_if(is_digits);
    map<const string,int>::iterator itor = wordmap.begin();
    while( (itor = find_if(itor,wordmap.end(),NotDigtilAndNotDouble)) != wordmap.end() )
    {
        wordmap.erase(itor++);
    }
}

2.

bool compare(char str1, char str2)
{
    string s1 = "([{";
    string s2 = ")]}";
    if (s1.find(str1) == s2.find(str2))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool charChecking(string s)
{
    bool balanced = true;
    stack<char> szStack;
    size_t index = 0;
    while(index < s.size() && balanced)
    {
        char symbol = s[index];
        if (symbol == '(' || symbol == '[' || symbol == '{')
        {
            szStack.push(symbol);
        }
        else
        {
            if (szStack.empty())
            {
                balanced = false;
            }
            else
            {
                char top = stack.pop();
                if (!compare(top , symbol))
                {
                    balanced = false;
                }
            }
        }
        index += 1;
    }
    if (balanced && szStack.empty())
    {
        return true;
    }
    else
    {
        return false;
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值