【发布时间】:2014-05-16 12:13:59
【问题描述】:
从对偶中选择 a[b]c[d][e]f[g];
我需要一个输出: acf
即删除所有 [] 以及它们之间的文本。
解决方案可以是 Oracle 或 C++ 函数。
在 C++ 中尝试过擦除功能,例如:
int main ()
{
std::string str ("a[b]c[d]e[f]");
std::cout << str << '\n';
while(1)
{
std::size_t foundStart = str.find("[");
//if (foundStart != std::string::npos)
std::cout << "'[' found at: " << foundStart << '\n';
str.begin();
std::size_t foundClose = str.find("]");
//if (foundClose != std::string::npos)
std::cout << "']' found at: " << foundClose << '\n';
str.begin();
str.erase (foundStart,foundClose);
std::cout << str << '\n';
}
return 0;
}
返回输出为:
a[b]c[d]e[f]
'[' found at: 1
']' found at: 3
ac[d]e[f]
'[' found at: 2
']' found at: 4
ac[f]
'[' found at: 2
']' found at: 4
ac
'[' found at: 18446744073709551615
']' found at: 18446744073709551615
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
提前致谢。
【问题讨论】: