擦除字符串中第一个括号“(”和最后一个括号“(”之间的所有字符,包括这些括号 C++
Erase all characters in string between the first parenthesis "(" andthe last parenthesis "(" including these parentheses C++
我无法删除第一个括号“(”和最后一个括号“(”之间的所有字符,包括它们。这是我用来使其工作的测试程序,但没有成功...
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
int count = 0;
for (std::string::size_type i = 0; i < str.size(); ++i)
{
if (str[i] == '(')
{
count += 1;
}
cout << "str[i]: " << str[i] << endl;
if (count <= 4)
{
str.erase(0, 1);
//str.replace(0, 1, "");
}
cout << "String: " << str << endl;
if (count == 4)
{
break;
}
cout << "Counter: " << count << endl;
}
cout << "Final string: " << str << endl;
system("PAUSE");
return 0;
}
在我上面展示的示例中,我的目标是(至少)获取字符串:
"1.32544e-7 0 0 ) ) )"
从原始字符串中提取
"( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )"
更准确地说,我想提取值
"1.32544e-7"
并转换为双精度以便在计算中使用。
我已经成功删除
" 0 0 ) ) )"
因为它是一种常数值。
谢谢!
将问题改写为 "I want to extract the double immediately following the last '('",C++ 翻译非常简单:
int main()
{
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
// Locate the last '('.
string::size_type pos = str.find_last_of("(");
// Get everything that follows the last '(' into a stream.
istringstream stream(str.substr(pos + 1));
// Extract a double from the stream.
double d = 0;
stream >> d;
// Done.
cout << "The number is " << d << endl;
}
(为清楚起见,省略了格式验证和其他簿记。)
您正在从 0
循环到字符串的长度并在进行时擦除承租人,这意味着您不会查看他们中的每一个。
一个小的改变会让你成功。不要更改您尝试迭代的字符串,只需记住您到达的索引即可。
using namespace std;
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
int count = 0;
std::string::size_type i = 0;
//^--------- visible outside the loop, but feels hacky
for (; i < str.size(); ++i)
{
if (str[i] == '(')
{
count += 1;
}
cout << " str[i]: " << str[i] << endl;
//if (count <= 4)
//{
//str.erase(0, 1);
//}
//^----------- gone
cout << "String: " << str << endl;
if (count == 4)
{
break;
}
cout << "Counter: " << count << endl;
}
return str.substr(i);//Or print this substring
这使我们留在第四个左括号中,因此如果我们没有到达字符串的末尾,我们需要一个额外的增量。
我无法删除第一个括号“(”和最后一个括号“(”之间的所有字符,包括它们。这是我用来使其工作的测试程序,但没有成功...
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
int count = 0;
for (std::string::size_type i = 0; i < str.size(); ++i)
{
if (str[i] == '(')
{
count += 1;
}
cout << "str[i]: " << str[i] << endl;
if (count <= 4)
{
str.erase(0, 1);
//str.replace(0, 1, "");
}
cout << "String: " << str << endl;
if (count == 4)
{
break;
}
cout << "Counter: " << count << endl;
}
cout << "Final string: " << str << endl;
system("PAUSE");
return 0;
}
在我上面展示的示例中,我的目标是(至少)获取字符串:
"1.32544e-7 0 0 ) ) )"
从原始字符串中提取
"( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )"
更准确地说,我想提取值
"1.32544e-7"
并转换为双精度以便在计算中使用。
我已经成功删除
" 0 0 ) ) )"
因为它是一种常数值。
谢谢!
将问题改写为 "I want to extract the double immediately following the last '('",C++ 翻译非常简单:
int main()
{
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
// Locate the last '('.
string::size_type pos = str.find_last_of("(");
// Get everything that follows the last '(' into a stream.
istringstream stream(str.substr(pos + 1));
// Extract a double from the stream.
double d = 0;
stream >> d;
// Done.
cout << "The number is " << d << endl;
}
(为清楚起见,省略了格式验证和其他簿记。)
您正在从 0
循环到字符串的长度并在进行时擦除承租人,这意味着您不会查看他们中的每一个。
一个小的改变会让你成功。不要更改您尝试迭代的字符串,只需记住您到达的索引即可。
using namespace std;
string str = "( 1221 ( 0 0 0 ) (1224.478541112155452 (1.32544e-7 0 0 ) ) )";
int count = 0;
std::string::size_type i = 0;
//^--------- visible outside the loop, but feels hacky
for (; i < str.size(); ++i)
{
if (str[i] == '(')
{
count += 1;
}
cout << " str[i]: " << str[i] << endl;
//if (count <= 4)
//{
//str.erase(0, 1);
//}
//^----------- gone
cout << "String: " << str << endl;
if (count == 4)
{
break;
}
cout << "Counter: " << count << endl;
}
return str.substr(i);//Or print this substring
这使我们留在第四个左括号中,因此如果我们没有到达字符串的末尾,我们需要一个额外的增量。