Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Cpp/C++;使用map';s_C++ - Fatal编程技术网

C++ Cpp/C++;使用map';s

C++ Cpp/C++;使用map';s,c++,C++,学校给我的任务是写一个程序来计算文件中使用的单词,但单词只能以大写/小写或“u”开头,并且只包括字母和数字0-9。 例如,我有这样的文件: #include <map> int main(){ map<string, int> vector int vector1; return 0; } #包括 int main(){ 映射向量 int向量1; 返回0; } 这是预期产出: 包括:1 地图:2 内部:3 字符串:1 病媒:1 向量1:1 回报:1 这是我

学校给我的任务是写一个程序来计算文件中使用的单词,但单词只能以大写/小写或“u”开头,并且只包括字母和数字0-9。 例如,我有这样的文件:

#include <map>
int main(){
   map<string, int> vector
   int vector1;
return 0;
}
#包括
int main(){
映射向量
int向量1;
返回0;
}
这是预期产出: 包括:1 地图:2 内部:3 字符串:1 病媒:1 向量1:1 回报:1

这是我已经得到的,但它计算每一行代码,而不仅仅是单词:

#include <iostream>
#include <map>
#include <fstream>
using namespace std;

typedef map<string, int> identifiers;

void countWords(istream &in, identifiers& w){
  string s;
  while(in >> s){
    ++w[s];
  }
}

int main()
{
fstream file;
file.open("test.txt",ios::in);
if(file){
   identifiers w;
   countWords(file, w);
   for(identifiers::iterator p = w.begin(); p!=w.end();++p){
       cout<<p->first<<" : "<<p->second<<endl;
   }
   
}
 
else{
   cout<<"Error opening the file"<<endl;
   exit(1);
}
file.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
typedef映射标识符;
void countWords(istream和in、标识符和w){
字符串s;
while(在>>s中){
++w[s];
}
}
int main()
{
流文件;
打开(“test.txt”,ios::in);
如果(文件){
标识符w;
countWords(文件,w);
对于(标识符::迭代器p=w.begin();p!=w.end();++p){

你可能没问问题。我想你应该写一个函数isWord(const std::string&),根据要求检查第一个字符和其他字符。你有什么问题吗?是的,我的错,我的问题是程序只计算“结构”但我已经通过扫描问题中所有可用的字母并将它们作为单词添加到地图中的功能解决了这个问题,但谢谢