题目:对于某一给定的时间段,用户登陆到某个计算机系统。在这段时间里,每当他们登陆到这个系统,他们的用户ID就被
自动记录到一个日志文件中。因为同一个用户可能登陆很多次,所以这个日志文件可能包含很多重复的用户ID。我们希望编写一个没有重复的用户ID的列表,并在这个列表中记录每个ID登陆了很多次
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int search(string & aUserId,vector<string> & userId);
string filename;
cout<<"please enter the file name :";
cin>>filename;
ifstream instream(filename.data());
if(!instream.is_open())
{
cout<<"Unable to ople the file:/n";
exit(-1);
}
vector<string> userId;
vector<int> loginCount;
string aUserId;
for( ; ; )
{
getline(instream,aUserId);
if(instream.eof())break;
int i;
if((i=search(aUserId,userId))>=userId.size())
{
userId.push_back(aUserId);
loginCount.push_back(1);
}
else
{
loginCount[i]++;
}
}
for(int j=0;j<userId.size();j++)
{
cout<<"the userId is :"<<userId[j]<<endl;
cout<<"the loginCount is :"<<loginCount[j]<<endl;
}
return 0;
}
int search(string & aUserId,vector<string> & userId)
{
int i=0;
for( ; ; )
{
if(i>=userId.size()||userId[i]==aUserId)break;
i++;
}
return i;
}