读文本文件的每一行 取每行最后一个空格后的子串

 

在Apriori实验中,助教给了类似这样的数据:

事务号 事务号 事务项

1   1   87
1   1   125
1   1   807
1   1   2084
1   1   2515
1   1   3344
1   1   3468
1   1   3623
1   1   3695
1   1   3903
1   1   4594
2   2   883
2   2   936
2   2   1351
2   2   1631
2   2   2970
2   2   3168
3   3   979
3   3   1334
3   3   1541
3   3   2036
3   3   2133
3   3   3478
3   3   3882
3   3   4190
3   3   4697
4   4   825
4   4   1501
4   4   2349
4   4   2503
4   4   3055
4   4   3486
4   4   3748
4   4   3807
 为抽取每行的事务项,也就是每行的第三列,可设计以下函数完成:

#include<string>

string getsubstr(string str){
       int position=0;  //当前串中最后一个空格后的字符串长度
       int len=str.length();
       bool end=false;
       for(int i=len-1;i>=0;i--){    //string的下标从0开始
               if((str[i]!=' ')&&(!end))
                  position++;
               else
                  end=true;
               }
      if(position<len)        
           return   str.substr(len-position,position);
      else
           return "error";
}

主函数调用:

int main(){
    string str;
ifstream ofs;
ofs.open(filepath);
while(getline(ofs,str)){
     cout<<"当前串"<<str<<endl;
     cout<<"当前子串"<<getsubstr(str)<<endl;
     }

system("pause"); 


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值