map_ints.cpp

本文通过一个具体的C++程序示例介绍了如何使用STL中的map容器来存储整数到字符串的映射,并演示了如何读取用户输入的数字并将其转换为对应的英文单词。

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #ifdef __BCPLUSPLUS__
#include <map.h>
#else
#include <map>
#endif

#include <iostream.h>
#include <string.h>

using namespace std;
typedef map<int, string, less<int>> INT2STRING;

void main(void)
 {
   // Create a map of ints to strings
   INT2STRING theMap;
   INT2STRING::iterator theIterator;
   string theString = "";
   int index, continue = 1;

   // Fill theMap with the digits 0 - 9, each mapped to its string counterpart
   // Note: value_type is a pair for maps...
   theMap.insert(INT2STRING::value_type(0,"Zero"));
   theMap.insert(INT2STRING::value_type(1,"One"));
   theMap.insert(INT2STRING::value_type(2,"Two"));
   theMap.insert(INT2STRING::value_type(3,"Three"));
   theMap.insert(INT2STRING::value_type(4,"Four"));
   theMap.insert(INT2STRING::value_type(5,"Five"));
   theMap.insert(INT2STRING::value_type(6,"Six"));
   theMap.insert(INT2STRING::value_type(7,"Seven"));
   theMap.insert(INT2STRING::value_type(8,"Eight"));
   theMap.insert(INT2STRING::value_type(9,"Nine"));
   // Read a Number from the user and print it back as words
   while (continue)
     {
       cout << "Enter /"q/" to quit, or enter a Number: ";
       cin >> theString;
       if(theString == "q")
         continue = 0;
       // extract each digit from the string, find its corresponding
       // entry in the map (the word equivalent) and print it
       for( index = 0; index < theString.length(); index++)
        {
          theIterator = theMap.find(theString[index] - '0');
          if(theIterator != theMap.end())   // is 0 - 9
            cout << (*theIterator).second << " ";
          else          // not 0 - 9
            cout << "[err] ";
        }
       cout << endl;
    }
 }

*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'E:\worker\keil\ARM\ARMCC\Bin' Rebuild target 'test-pid' assembling startup_stm32f407xx.s... compiling stm32f4xx_hal_timebase_tim.c... compiling stm32f4xx_it.c... compiling usart.c... compiling dma.c... compiling stm32f4xx_hal_tim_ex.c... compiling stm32f4xx_hal_tim.c... compiling gpio.c... compiling stm32f4xx_hal_msp.c... compiling main.c... compiling can.c... compiling freertos.c... compiling stm32f4xx_hal_pwr_ex.c... compiling stm32f4xx_hal_flash_ramfunc.c... compiling stm32f4xx_hal_flash_ex.c... compiling stm32f4xx_hal_rcc_ex.c... compiling stm32f4xx_hal_flash.c... compiling stm32f4xx_hal_gpio.c... compiling stm32f4xx_hal_dma.c... compiling stm32f4xx_hal_rcc.c... compiling stm32f4xx_hal_can.c... compiling stm32f4xx_hal_dma_ex.c... compiling stm32f4xx_hal_pwr.c... compiling alg_pid.cpp... compiling drv_math.cpp... compiling croutine.c... compiling event_groups.c... compiling list.c... compiling queue.c... compiling stream_buffer.c... compiling tasks.c... compiling timers.c... compiling stm32f4xx_hal_cortex.c... compiling stm32f4xx_hal_exti.c... compiling system_stm32f4xx.c... compiling drv_uart.c... compiling drv_can.c... compiling dvc_serialplot.cpp... compiling stm32f4xx_hal.c... compiling cmsis_os.c... compiling drv_bsp.c... compiling heap_4.c... compiling dvc_motor.cpp... compiling stm32f4xx_hal_uart.c... compiling port.c... linking... test-pid\test-pid.axf: Error: L6218E: Undefined symbol MX_FREERTOS_Init() (referred from main.o). Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 1 error messages. "test-pid\test-pid.axf" - 1 Error(s), 0 Warning(s). Target not created. Build Time Elapsed: 00:00:11,主文件中是有定义的
最新发布
07-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值