C++ 从字符串中提取整数
C++ extracting ints from a string
我正在尝试从字符串中提取整数,例如用户输入可以是 5ft12in
或 5'12"`。
但是,当我的输入是 5ft1in
时代码有效,但当它是 5ft12in
时无效。
我想遍历整个字符串并提取 3 个数字,例如:
feet = 5
inches 1 =1
inches 2 = 2
但我似乎找不到问题。
我认为有一种方法可以使用 char
将输入转换为 stringstream
然后 peek
通过整个字符串,但我不太确定如何。
string feet = "";
string inches = "";
string inches2 = "";
for (int i = 0; i < height.size(); ++i) {
if (isdigit(height[i]) && (feet.empty())) {
feet = height[i];
} // end if
else if ((isdigit(height[i]) && (!feet.empty()))) {
inches = height[i];
}
else if ((isdigit(height[i]) && (!feet.empty() && (!inches.empty())))) {
inches2 = height[i];
} // end else if
}//end for
cout << "String of feet : " << feet << endl;
cout << "String of inches 1 : " << inches << endl;
cout << "String of inches 2 : " << inches2 << endl;
在您的第二个 if 条件中,您没有检查 inches 的值是否为空。因此,当在第二个 if 条件中检查字符串“5ft12in”中的“2”时,它非常满足它。因此,导致再次将值“2”存储在 inches 中,而您实际上想要存储在 inches2.
中
解决方法:
string feet = "";
string inches = "";
string inches2 = "";
for(int i = 0; i < height.size(); ++i)
{
if (isdigit(height[i]) && (feet.empty())) {
feet = height[i];
} // end if
else if ((isdigit(height[i]) && (!feet.empty()) && (inches.empty())) {
inches = height[i];
}
else if ((isdigit(height[i]) && (!feet.empty() &&
(!inches.empty())))) {
inches2 = height[i];
} // end else if
}//end for
cout << "String of feet : " << feet << endl;
cout << "String of inches 1 : " << inches << endl;
cout << "String of inches 2 : " << inches2 << endl;
你根本不需要循环。
查看std::string
的find_first_of()
and find_first_not_of()
方法,例如:
std::string feet;
std::string inches;
std::string::size_type i = height.find_first_not_of("0123456789");
feet = height.substr(0, i);
std::string::size_type j = find_first_of("0123456789", i);
i = height.find_first_not_of("0123456789", j);
if (i == std::string::npos) i = height.size();
inches = height.substr(j, i-j);
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;
但是,使用 std::regex()
可以更好地处理这种模式搜索(仅限 C++11 及更高版本):
#include <regex>
std::string feet;
std::string inches;
std::smatch sm;
std::regex_match(height, sm, std::regex("(\d+)\D+(\d+)\D+"));
if (sm.size() == 3)
{
feet = sm.str(1);
inches = sm.str(2);
}
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;
#include <cstdio>
int feet, inches;
std::sscanf(height.c_str(), "%d%*[^0-9]%d", &feet, &inches);
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;
我正在尝试从字符串中提取整数,例如用户输入可以是 5ft12in
或 5'12"`。
但是,当我的输入是 5ft1in
时代码有效,但当它是 5ft12in
时无效。
我想遍历整个字符串并提取 3 个数字,例如:
feet = 5
inches 1 =1
inches 2 = 2
但我似乎找不到问题。
我认为有一种方法可以使用 char
将输入转换为 stringstream
然后 peek
通过整个字符串,但我不太确定如何。
string feet = "";
string inches = "";
string inches2 = "";
for (int i = 0; i < height.size(); ++i) {
if (isdigit(height[i]) && (feet.empty())) {
feet = height[i];
} // end if
else if ((isdigit(height[i]) && (!feet.empty()))) {
inches = height[i];
}
else if ((isdigit(height[i]) && (!feet.empty() && (!inches.empty())))) {
inches2 = height[i];
} // end else if
}//end for
cout << "String of feet : " << feet << endl;
cout << "String of inches 1 : " << inches << endl;
cout << "String of inches 2 : " << inches2 << endl;
在您的第二个 if 条件中,您没有检查 inches 的值是否为空。因此,当在第二个 if 条件中检查字符串“5ft12in”中的“2”时,它非常满足它。因此,导致再次将值“2”存储在 inches 中,而您实际上想要存储在 inches2.
中解决方法:
string feet = "";
string inches = "";
string inches2 = "";
for(int i = 0; i < height.size(); ++i)
{
if (isdigit(height[i]) && (feet.empty())) {
feet = height[i];
} // end if
else if ((isdigit(height[i]) && (!feet.empty()) && (inches.empty())) {
inches = height[i];
}
else if ((isdigit(height[i]) && (!feet.empty() &&
(!inches.empty())))) {
inches2 = height[i];
} // end else if
}//end for
cout << "String of feet : " << feet << endl;
cout << "String of inches 1 : " << inches << endl;
cout << "String of inches 2 : " << inches2 << endl;
你根本不需要循环。
查看std::string
的find_first_of()
and find_first_not_of()
方法,例如:
std::string feet;
std::string inches;
std::string::size_type i = height.find_first_not_of("0123456789");
feet = height.substr(0, i);
std::string::size_type j = find_first_of("0123456789", i);
i = height.find_first_not_of("0123456789", j);
if (i == std::string::npos) i = height.size();
inches = height.substr(j, i-j);
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;
但是,使用 std::regex()
可以更好地处理这种模式搜索(仅限 C++11 及更高版本):
#include <regex>
std::string feet;
std::string inches;
std::smatch sm;
std::regex_match(height, sm, std::regex("(\d+)\D+(\d+)\D+"));
if (sm.size() == 3)
{
feet = sm.str(1);
inches = sm.str(2);
}
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;
#include <cstdio>
int feet, inches;
std::sscanf(height.c_str(), "%d%*[^0-9]%d", &feet, &inches);
std::cout << "feet : " << feet << std::endl;
std::cout << "inches : " << inches << std::endl;