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;
}
}