2014-10-30 74 views
-4

我有一个文本文件,内容是这样的:从字符串中提取整数并转换为整数?

Department Max 
Bob 3 5 6 2 8 F1 
Frank 4 8 8 8 8 F2 
Jerry 7 9 9 0 9 F3 
William 0 0 12 13 14 F2 
Buck 3 4 10 8 8 4 6 F4 
Greg 1 2 1 4 2 F1 
Mike 3 4 4 8 4 F2 

而忽略了名字,我怎么读这些线和提取这些整数作为独立的整数,所以我可以把它们相加在一起?

到目前为止,我有这样的:

for (int i = 0; i < 10; i++) //Loop that checks the employee and their paygrade, 10 times for 10 lines of data 
    { 
     getline(inFile, s); //Gets next line in Salary.txt 
     string payGrade = s.substr(s.length() - 2, 2); //Checks the last two letters of the line to see paygrade 
     if (payGrade == "F1") 
     { 
      auto pos = s.find(' ', s.find('"')); 
      istringstream iss(s.substr(pos)); 


      F1employeeCounter++; 
     } 
     if (payGrade == "F2") 
     { 

      F2employeeCounter++; 
     } 
     if (payGrade == "F3") 
     { 

      F3employeeCounter++; 
     } 
     if (payGrade == "F4") 
     { 

      F4employeeCounter++; 
     } 
    } 

基本上我要检查什么类型的“薪级”每个员工。有四种不同类型的薪酬等级:F1,F2,F3,F4,每种薪酬等级的工作时间都有不同的支付方式。

+0

你可以用'atoi'将字符串转换为整数。您当然仍然需要拆分字符串。 – 2014-10-30 21:37:05

+0

http://stackoverflow.com/questions/7663709/convert-string-to-int-c + http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c可能有帮助 – matsjoyce 2014-10-30 21:37:21

+0

如果你有一个在线搜索很容易的问题,你可能需要先试着解决它。如果你之后仍然有问题,告诉我们你试过的东西 – PeterT 2014-10-30 21:45:31

回答

0
#include <iostream> 
#include <string> 
#include <sstream> 

int main() 
{ 
    std::istringstream iss("Bo 3 3 6 2 8\nFred 7 8 5 8 8\nJosh 7 9 4 0 1"); 

    while (iss.good()) //will exit this loop when there is no more text to read 
    { 
     std::string line; 

     //extract next line from iss 
     std::getline(iss, line); 

     //construct string stream from extracted line 
     std::stringstream ss(line); 

     std::string name; 
     int sum = 0; 
     int i = 0; 

     //extract name 
     ss >> name; 

     while(ss.good()) 
     { 
      //extract integer 
      ss >> i; 

      //add to the sum 
      sum += i; 
     } 

     //output name and sum 
     std::cout << name << ":" << sum << std::endl; 
    } 
} 

输出:

Bo:22 
Fred:36 
Josh:21 
1

这里遵循它如何,如果你事先该文件中的文本的结构总是会知道长得一样在你的例子做一个例子。

传统上,流在C++中用于从文本中解析数据(例如整数)。在这种情况下,围绕std::istringstream缠绕的流迭代器std::istream_iterator用于解析整数标记。此外,标准算法std::accumulate用于对解析的整数进行求和。

std::ifstream input{"my_file"}; 

// For every line read: 
for (std::string line; std::getline(input, line);) { 
    // Find position of first space after first citation character ("). 
    auto pos = line.find(' ', line.find('"')); 

    // Use position to copy relevant numbers part into a string stream. 
    std::istringstream iss{line.substr(pos)}; 

    // Accumulate sum from parsed integers. 
    auto sum = std::accumulate(std::istream_iterator<int>{iss}, 
           std::istream_iterator<int>{}, 
           0); 

    /* Do something with sum... */ 
} 

Live example

+0

这没有考虑到每行可变数量的整数。 – 2014-10-30 21:47:19

+0

@cmbasnett OP不会说姓名*不会有姓,或者每个4:th行都不会说'披萨时间'......这个例子清楚地显示每行5个数字。 – Snps 2014-10-30 21:58:29

+0

自从我发表评论以来,答案发生了变化,此解决方案非常好。 – 2014-10-31 00:39:04