📄 subject_60015.htm
字号:
<p>
序号:60015 发表者:ljl 发表日期:2003-11-11 15:39:29
<br>主题:c++ primer上的程序,请帮忙看看,谢谢!
<br>内容:#pragma warning(disable : 4786) <BR>#include <map><BR>#include <vector><BR>#include <iostream><BR>#include <string><BR>#include <functional><BR><BR>using namespace std;<BR><BR>void main()<BR>{<BR> map< string, string > trans_map;<BR> typedef map< string, string >::value_type valType;<BR><BR> //第一个权宜之计:将转换对固定写在代码中<BR> trans_map.insert( valType( "gratz", "grateful" ));<BR> trans_map.insert( valType( "em ", "them" ));<BR> trans_map.insert( valType( "cuz", "because" ));<BR> trans_map.insert( valType( "nah", "no" ));<BR> trans_map.insert( valType( "sez", "says" ));<BR> trans_map.insert( valType( "tanx", "thanks" ));<BR> trans_map.insert( valType( "wuz", "was" ));<BR> trans_map.insert( valType( "pos", "suppose" ));<BR><BR> //ok: 先是trans_map<BR> map< string, string >::iterator it;<BR> cout << "Here is our transformation map: \n\n";<BR> for( it = trans_map.begin (); it != trans_map.end(); ++it )<BR> {<BR> cout << "key: " << (*it).first << "\t"<BR> << "value: " << (*it).second << "\n";<BR> }<BR> cout << "\n\n";<BR> <BR> //第二个权宜之计:固定写入文字<BR> string textarray[14] = { "nah", "I", "sez", "tanx", "cuz", "I",<BR> "wuz", "pos", "to", "not", "cuz", "I", "wuz", "gratz" };<BR> vector< string > text(textarray, textarray + 14);<BR> vector< string >::iterator iter;<BR><BR> //ok: show text<BR> cout << "Here is our original string vector: \n\n";<BR> int cnt = 1;<BR> for( iter = text.begin(); iter != text.end (); ++iter, ++cnt)<BR> {<BR> cout << *iter << ( cnt % 8 ? " " : "\n");<BR> }<BR> cout << "\n\n\n";<BR><BR> //饱含统计信息的map ---动态生成<BR> map< string, int > stats;<BR> typedef map< string, int >::value_type statsValType;<BR><BR> //ok: 真正的map工作---程序的核心<BR> for( iter = text.begin (); iter != text.end(); ++iter)<BR> {<BR> if(( it = trans_map.find (*iter)) != trans_map.end ())<BR> {<BR> if ( stats.count (*iter))<BR> stats[ *iter ] +=1;<BR> else stats.insert(statsValType( *iter, 1));<BR> *iter = (*it).second;<BR> }<BR> }<BR><BR> //ok: 先是被转换后的vector<BR> cout << "Finally, here are our statistics: \n\n";<BR> map<string, int, less<string>, allocator>::iterator siter;<BR> for( siter = stats.begin (); siter != stats.end(); ++siter)<BR> {<BR> cout << (*siter).first << " "<BR> << "was transformed "<BR> << (*siter).second<BR> << ((*siter).second == 1)<BR> << " time\n" ;//: " times\n";<BR> }<BR>}<BR><BR><BR><BR>2003-12-29 12:19:50
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -