【问题标题】:Increase efficiency - parse integers from string C++提高效率 - 从字符串 C++ 解析整数
【发布时间】:2017-03-02 19:43:04
【问题描述】:

我有一个字符串:

12:56:72

我需要分别获取 3 个数字(12、56 和 72)。

我在做:

int i=0,j=0,k=0,n, p[3]; // p[i] stores each ith value
char x[], b[];
x="12:34:56";
n=strlen(x);
while(i<strlen){
        b[k]=x[i];
        ++i;
        ++k;
        if(x[i==]':'){
            p[j]=int(b);
            ++j;
            ++i;
            k=0;
            char temp[] = b;
            b=new char[];
            delete temp;
            }
        }

这可以更有效地完成吗?

【问题讨论】:

  • 你是如何得到字符串的?如果它来自输入流,您可以直接将其读入整数。
  • 我使用 fstream 从输入流中获取它。如何直接获取整数?
  • 数字是两位数还是一位数?
  • 它们的长度可以是一位数。
  • 代码中有太多错误,更高效地执行它是没有意义的。发布可编译和运行的代码。

标签: c++ string parsing c++11 int


【解决方案1】:

要“更有效率”,您必须进行概要分析。

这是另一个解决方案:

const std::string test_data("01:23:45");
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
char separator;
std::istringstream input(test_data);
// Here's the parsing part
input >> hours >> separator >> minutes >> separator >> seconds;

这是否“更有效”,必须进行衡量。
它看起来更简单,更安全。

编辑 1:方法 2
处理器不喜欢循环或分支,所以我们可以尽量减少。 此优化假设完美输入为字符串。

static const char test_data[] = "01:23:45";
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
char c;
unsigned int index = 0;
hours = test_data[index++] - '0';
if (test_data[index] != ':')
{
  hours = hours * 10 + test_data[index++] - '0';
}
++index; // Skip ':'
minutes = test_data[index++] - '0';
if (test_data[index] != ':')
{
  minutes = minutes * 10 + test_data[index++] - '0';
}
++index; // Skip ':'
seconds = test_data[index++] - '0';
if (test_data[index] != ':')
{
  seconds = seconds * 10 + test_data[index++] - '0';
}

对于最高优化,您必须做出一些假设。另一个假设是字符编码是 UTF8 或 ASCII,例如'1' - '0' == 1。

【讨论】:

    【解决方案2】:

    由于您是从文件流中读取数据,因此您可以使用该流的operator &gt;&gt; 来发挥您的优势。当它将数据输入int 时,它将继续读取文件,直到遇到对int 无效的字符。这意味着它将读取整数部分并将冒号留在文件中。由于这种行为,我们可以使用

    ifstream fin("filename.ext");
    int a, b, c;
    char colon;
    while(fin >> a >> colon >> b >> colon >> c)
    {
        // do stuff with a, b and c
    }
    

    只要文件中有12:56:72格式的行,这将逐行读取文件。

    【讨论】:

    • 这行得通,谢谢。在我看到(并尝试)这个之前,我尝试了 Thomas 的方法。由于我的代表,它没有显示我的赞成票。
    • @AgentWeirdo 没问题。很高兴为您提供帮助
    【解决方案3】:

    它看起来像一个时钟,但仍然可以使用 sscanf 扫描字符串:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       char myclock[] = "10:11:12";
       int hours, minutes, seconds;
       sscanf(myclock, "%d:%d:%d", &hours, &minutes, &seconds);
       cout << hours << endl;
       cout << minutes << endl;
       cout << seconds << endl;
       return 0;
    }
    

    【讨论】:

      【解决方案4】:

      您可以考虑使用 C++ 函数。

      #include <string>
      #include  <iostream>
      using namespace std;
      
      int main() {
          size_t i=0,j=0,k=0, p[3]; // p[i] stores each ith value
          string x("12:34:56");
          for (j=0; j<3; j++) {
              p[j] = stoi(x.substr(k), &i);
              k += i+1;
          }
      
          for (j =0; j < 3; j++)
              cout << p[j] << endl;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 2013-10-23
        • 1970-01-01
        • 2011-04-03
        • 2021-01-23
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多