【问题标题】:C++ extracting ints from a stringC++ 从字符串中提取整数
【发布时间】:2017-05-18 19:35:26
【问题描述】:

我正在尝试从字符串中提取整数,例如用户输入可能是 5ft12in 或 5'12"`。

但是,当我的输入为 5ft1in 时,该代码有效,但在输入 5ft12in 时无效。

我想遍历整个字符串并提取3个数字,例如:

feet = 5 
inches 1 =1 
inches 2 = 2   

但我似乎找不到问题。

我认为有一种方法可以将输入转换为stringstream,然后使用char 将整个字符串转换为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;

【问题讨论】:

  • std::istringstream 应该这样做。
  • 我真的是一个初学者,所以如果我把它放在一个 ss 中; istringstream iss1("5ft10in");我将如何提取 0? while (ch = iss1.peek()) { if (!isdigit(ch)) iss1.ignore();否则 { iss1 >> i; cout
  • 找到“ft”和“in”的位置,查看它们之前和之间的子串。
  • 我可以这样做,但用户也可以输入 5'12'' 所以必须遍历整个字符串并仅返回整数
  • 看看std::stringfind_first_of()find_first_not_of()方法。

标签: c++ sstream


【解决方案1】:

你根本不需要循环。

查看std::stringfind_first_of()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;

std::sscanf():

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

【讨论】:

  • 感谢您的及时响应 Remy,非常感谢,代码有效。我只是一个初学者,所以像这样的论坛对我帮助很大。再次感谢。
【解决方案2】:

在您的第二个 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多