如何循环使用流C++中的字符
How to loop through characters in stream C++
因此,我编写的代码从用户那里接收一个文件,删除所有标点符号。将文件设为小写,并查找所有唯一的单词。这个问题不正常,我不知道为什么。我没有打印出正确数量的唯一值
// convert charater to lowercase
void convert(char& ch){
ch = tolower(ch);
}
int main(){
typedef map<string,int> siMap;
typedef pair<siMap::iterator, bool>ibPair;
typedef siMap::value_type kvpair;
siMap myMap;
string fileName1;
string fileName2;
string value;
char ch;
// get user input
cout << "Please enter the first file Name: "; //asking for a file name
cin >> fileName1;
ifstream infile1;
infile1.open(fileName1.c_str());
//remove all punctuations
while (infile1.get(ch))
{
if (isalpha(ch) || isspace(ch)){
convert(ch);
}
else{
infile1.ignore(ch);
}
infile1 >> value;
myMap[value]++;
}
/*
for (int i = 0; i < 10; i++) {
cout << infile1[i];
} */
cout << " Map size is :" << myMap.size() << endl;
cout << "Please enter the second file Name: "; //asking for a file name
cin >> fileName2;
ifstream infile;
infile.open(fileName2.c_str());
}
您能提供第一个输入文件吗?还记得在完成文件后关闭它。对于调试部分,您可能需要使用映射迭代器来找出缺少的单词。
// convert charater to lowercase
void convert(char& ch)
{
ch = tolower(ch);
}
int main()
{
typedef map<string,int> siMap;
typedef pair<siMap::iterator, bool>ibPair;
typedef siMap::value_type kvpair;
siMap myMap;
string fileName1;
string fileName2;
string value;
char ch;
// get user input
cout << "Please enter the first file Name: "; //asking for a file name
cin >> fileName1;
std::ifstream ifs(fileName1.c_str());
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
//remove all punctuations
for(std::string::iterator it = content.begin(); it != content.end(); ++it)
{
if (isalpha(*it))
{
convert(*it);
}
else
{
*it = ' ';
}
}
//now working on your map
std::stringstream ss;
ss.str (content);
string singleWord;
while (ss >> singleWord)
{
myMap[singleWord]++;
}
cout << " Map size is :" << myMap.size() << endl;
cout << "blahblah" << endl;
for (std::map<string,int>::iterator it=myMap.begin(); it!=myMap.end(); ++it)
std::cout << it->first << " => " << it->second << 'n';
cout << "Please enter the second file Name: "; //asking for a file name
cin >> fileName2;
ifstream infile;
infile.open(fileName2.c_str());
}
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存