typedef pair<char, int> CHARPOSITION;
std::string RemoveBracketsAndContents(std::string Input)
{
stack<CHARPOSITION> staCheak;
for (size_t i = 0; i < Input.length(); i++)
{
char ch = Input[i];
switch(ch)
{
case '(':
case '[':
case '{':
case '<':
{
staCheak.push(make_pair(Input[i],i));
break;
}
case ')':
case ']':
case '}':
case '>':
{
if (!staCheak.empty())
{
CHARPOSITION charPos = staCheak.top();
if ( (Input[i] == ')' && charPos.first == '(' ) || (Input[i] == ']' && charPos.first == '[' )
|| (Input[i] == '>' && charPos.first == '<' ) || (Input[i] == '}' && charPos.first == '{' ) )
{
staCheak.pop();
size_t Left = charPos.second;
Input.replace(Left,i - Left + 1, i - Left + 1, ' ');
}
}
break;
}
}
}
return Input;
}
C++ 字符串删除匹配括号及括号里的内容
最新推荐文章于 2022-03-31 22:04:53 发布