是的,我目前正在学习向量。我正在尝试读取一个文本文件,计算唯一单词的数量,然后输出一个文本文件(稍后会做)。可以使用一些帮助来了解正在发生的事情以及为什么/如何解决?
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
string toLower(string str);
string eraseNonAlpha(string Ast);
string wordWasher(string str);
int countUniqenum(vector<string>&v);
int main()
{
ifstream inputStream; //the input file stream
string word; //a word read in from the file
string words=wordWasher(word);
inputStream.open("alpha.txt");
if (inputStream.fail())
{
cout<<"Can't find or open file"<<endl;
cout<<"UR a loser"<<endl;
system("pause");
return 0;
}//end if
while (!inputStream.eof())
{
inputStream>>words;
}//end loop
vector<string> v;
v.push_back(words);
int unique =countUniqenum(v);
cout<<unique<<endl;
inputStream.close();
system("pause");
return 0;
}
string toLower(string str)
{
for(int i=0;i<str.length();i++)
{
if (str[i]>='A'&& str[i]<='Z')
str[i]=str[i]+32;
}
return str;
}
string eraseNonAlpha(string str)
{
for(int i=0;i<str.length();i++)
{
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))
{str.erase(i,1);
i--;
}
}
return str;
}
string wordWasher(string str)
{ str=eraseNonAlpha(str);
str=toLower(str);
return str;
}
int countUniqenum(vector<string>&v)
{ int count=0;
for(int i=0;i<v.size();i++)
{
if(v[i]!=v[i+1])
count++;
}
return count;
}