map, fstream, error


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


#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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的实现,使用了C++11的新特性,如auto和range-based for循环: ```c++ #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; const string dictFile = "dictionary.txt"; // 从文件中读入字典 void loadDict(map<string, string>& dict) { ifstream fin(dictFile); if (!fin.is_open()) { cerr << "Error: unable to open file " << dictFile << endl; return; } string word, meaning; while (fin >> word >> meaning) { dict[word] = meaning; } fin.close(); } // 将字典写入文件 void saveDict(const map<string, string>& dict) { ofstream fout(dictFile); if (!fout.is_open()) { cerr << "Error: unable to create file " << dictFile << endl; return; } for (const auto& p : dict) { fout << p.first << " " << p.second << endl; } fout.close(); } // 查找单词 void lookupWord(const map<string, string>& dict) { string word; cout << "Enter word to lookup: "; cin >> word; auto it = dict.find(word); if (it == dict.end()) { cout << "Not found." << endl; } else { cout << it->first << ": " << it->second << endl; } } // 添加或修改单词 void addOrUpdateWord(map<string, string>& dict) { string word, meaning; cout << "Enter word to add/update: "; cin >> word; cout << "Enter meaning: "; cin >> meaning; dict[word] = meaning; saveDict(dict); } // 删除单词 void deleteWord(map<string, string>& dict) { string word; cout << "Enter word to delete: "; cin >> word; auto it = dict.find(word); if (it == dict.end()) { cout << "Not found." << endl; } else { dict.erase(it); saveDict(dict); } } int main() { map<string, string> dict; loadDict(dict); int choice; do { cout << "1. Lookup word" << endl; cout << "2. Add/update word" << endl; cout << "3. Delete word" << endl; cout << "4. Exit" << endl; cout << "Enter your choice (1-4): "; cin >> choice; switch (choice) { case 1: lookupWord(dict); break; case 2: addOrUpdateWord(dict); break; case 3: deleteWord(dict); break; case 4: break; default: cout << "Invalid choice." << endl; break; } } while (choice != 4); return 0; } ``` 这个程序使用了一个名为“dictionary.txt”的文件来存储字典内容,每行一个单词和它的汉字解释,中间用空格隔开。在程序启动时,会从这个文件中读入字典,并将其存储在一个map对象中。每次添加、修改或删除单词后,程序都会将map对象写回到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值