在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");
}