map, fstream, error

本文介绍了一个C++程序,该程序从一个已排序的文本文件中读取单词,并利用标准模板库(STL)中的多重映射(multimap)来统计以特定字母开头的单词数量。此外,还演示了如何查找指定范围内的单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


这个程序看不是很懂~~~


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

int main ()  
{ 
	typedef multimap<char,string> M1; 
	typedef M1::value_type v_t1; 
	M1 m1; 
	typedef multimap<string,char,less<string> > M2; 
	typedef M2::value_type v_t2; 
	M2 m2; 
	
	string word; 
	int counter = 0; 
	
	ifstream In("words.txt"); 
     if ( In.good() ) 
	 { 
         while(1) 
         { 
             getline(In,word); 
             char ch = word.at(0); 
             // file is sorted 
             if ( ch != 'A' && ch != 'a' ) 
                 break; 
             else 
             { 
                 // for counting of words 
                 m1.insert(v_t1(ch,word)); 
                 // for upper-lower bound 
                 m2.insert(v_t2(word,ch)); 
             } 
             counter++; 
         } 
         In.close(); 
     } 
	 
     cout << "System Dictionary consists " << counter
		 << " words,\nwith first letter 'a' or 'A'" 
		 << endl; 
     cout << m1.count('A') << " words start with 'A'" 
          << endl; 
	 cout << m1.count('a') << " words start with 'a'" 
		 << endl; 
	 
     M2::iterator low = m2.lower_bound("Aba"); 
     M2::iterator upp = m2.upper_bound("Abe"); 
     cout << "Range of the words from 'Aba' to 'Abe':" 
		 << endl; 
     while ( low != upp ) 
     { 
         cout << (*low).first << endl; 
         low++; 
     } 
     return 0; 
} 
//OUTPUT: 
// System Dictionary consists 3577 words, 
// with first letter 'a' or 'A' 
// 491 words start with 'A' 
// 3086 words start with 'a' 
// Range of the words from 'Aba' to 'Abe': 
// Ababa 
// Abba 
// Abbott 
// Abby 
// Abe 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值