首页 > 其他分享 >SAP ALV模板

SAP ALV模板

时间:2023-10-20 14:24:31浏览次数:56  
标签:GW SY &---------------------------------------------------------------------

效果图:ALV显示自动优化,更新后数据自动回写内表,日期栏位输入帮助,获取长文本数据

 

 代码如下:

 

REPORT Z_ALV_MODULE.

TABLES:VEPO,EKKO,LIPS,LIKP.
*ALV结构
TYPES: BEGIN OF TY_ALV,
  BOX TYPE C,
  ZFMK TYPE C,
  BOLNR TYPE LIKP-BOLNR,
  VBELN TYPE LIKP-VBELN,
  POSNR TYPE LIPS-POSNR,
  MATNR TYPE LIPS-MATNR,
  VGBEL TYPE LIPS-VGBEL,
  VGPOS TYPE LIPS-VGPOS,
  ERDAT TYPE LIKP-ERDAT,
  LFIMG TYPE LIPS-LFIMG,
  DAYS TYPE I,
  CODE TYPE STRING,
  DOWN TYPE C,
  DD_HANDLE TYPE INT4,"下拉列表
END OF TY_ALV.
TYPES:BEGIN OF TY_BOLNR,
       ZBOLNR TYPE ZBOLNR,
       ZCLANO TYPE ZCLANO,
 END OF TY_BOLNR.

*----------------------------------------------------------------------*
*  定义ALV数据对象
*----------------------------------------------------------------------*
DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
      W_FIELDCAT  TYPE SLIS_FIELDCAT_ALV,
      W_LAYOUT    TYPE SLIS_LAYOUT_ALV,
      GS_SETTINGS TYPE LVC_S_GLAY.
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
      GW_FIELDCAT TYPE LVC_S_FCAT,
      GW_LAYOUT   TYPE LVC_S_LAYO,
      GT_DDVAL TYPE LVC_T_DROP,
      GW_DDVAL TYPE LVC_S_DROP,
      GT_EVENTS TYPE SLIS_T_EVENT,
      GW_EVENTS TYPE SLIS_ALV_EVENT.

DATA: GW_ALV TYPE TY_ALV,
            GT_ALV TYPE STANDARD TABLE OF TY_ALV,
            ZNAME TYPE THEAD-TDNAME.
DATA: GT_BOLNR TYPE TABLE OF TY_BOLNR,
           WA_BOLNR TYPE TY_BOLNR.
DATA: STBL TYPE LVC_S_STBL,
           G_GRID           TYPE REF TO CL_GUI_ALV_GRID.
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       类定义
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER DEFINITION."类定义
  PUBLIC SECTION.
    METHODS:
*  第一屏alv数据改变
      HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID
        IMPORTING E_MODIFIED ET_GOOD_CELLS.
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*       类实现
*----------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION."类实现
  METHOD HANDLE_DATA_CHANGED.
    PERFORM HANDLE_DATA_CHANGED .
    IF E_MODIFIED = 'X'.
        STBL-ROW = 'X'." 基于行的稳定刷新
        STBL-COL = 'X'." 基于列稳定刷新
        CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY
          EXPORTING
            IS_STABLE = STBL.
    ENDIF.
  ENDMETHOD.
ENDCLASS. "lcl_event_handler IMPLEMENTATION
*&---------------------------------------------------------------------*
*&      选择屏幕定义
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

SELECT-OPTIONS:
                S_BOLNR FOR LIKP-BOLNR.          "提单
SELECTION-SCREEN SKIP.
  PARAMETERS:P_DP RADIOBUTTON GROUP G1 DEFAULT 'X',
                          P_ED RADIOBUTTON GROUP G1 .
SELECTION-SCREEN END OF BLOCK B1.

*&---------------------------------------------------------------------*
*&      执行查询
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM FRM_GET_DATA.
  PERFORM FRM_ALV_DISPLAY.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       获取数据源
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .
  SELECT DISTINCT
    L~BOLNR,
    L~VBELN,
    L~ERDAT,
    P~POSNR,
    P~MATNR,
    P~VGBEL,
    P~VGPOS,
    P~LFIMG
    INTO CORRESPONDING FIELDS OF TABLE @GT_ALV
    FROM LIKP AS L
    LEFT JOIN LIPS AS P ON P~VBELN = L~VBELN
    WHERE L~BOLNR IN @S_BOLNR.
    CLEAR:GW_ALV.
  LOOP AT GT_ALV INTO GW_ALV.
    CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
      EXPORTING
        i_date_from = GW_ALV-ERDAT
        i_date_to   = sy-datum "
      IMPORTING
        e_days      = GW_ALV-DAYS."天数
    CLEAR:ZNAME.
    ZNAME = GW_ALV-VBELN.
*    PERFORM FRM_READ_TXT USING 'F01' ZNAME 'EKKO' SY-LANGU CHANGING TNM.
*    IF TNM IS NOT INITIAL.
*      GW_ALV-CODE = TNM+0(3).
*    ENDIF.
    GW_ALV-CODE = 'XXXYMMZMOBQ'.
    MODIFY GT_ALV FROM GW_ALV.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       显示ALV报表
*----------------------------------------------------------------------*
FORM FRM_ALV_DISPLAY .
  PERFORM FRM_FILL_FIELDCAT.
  PERFORM FRM_ALV_LAYOUT.
  PERFORM FRM_ALV_INIT_SETTINGS.
  PERFORM CREATE_DROPDOWN_VALUES.
  PERFORM FRM_SET_ALV_EVENTS CHANGING GT_EVENTS.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    I_CALLBACK_PROGRAM = SY-REPID
    I_CALLBACK_PF_STATUS_SET = 'PF_STATUS_SET'
    I_CALLBACK_USER_COMMAND  = 'FRM_USER_COMMAND'
    I_GRID_SETTINGS  = GS_SETTINGS
    IS_LAYOUT_LVC      = GW_LAYOUT
    IT_FIELDCAT_LVC    = GT_FIELDCAT
    IT_EVENTS              = GT_EVENTS
    I_SAVE             = 'A'
  TABLES
    T_OUTTAB           = GT_ALV
  EXCEPTIONS
    PROGRAM_ERROR      = 1
    OTHERS             = 2.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_FILL_FIELDCAT
*&---------------------------------------------------------------------*
*       填充报表结构
*----------------------------------------------------------------------*
FORM FRM_FILL_FIELDCAT .
  PERFORM FM_APPEND_FIELDCAT USING 'ZFMK' '提单标识'.
  PERFORM FM_APPEND_FIELDCAT USING 'BOLNR' '提单号码'.
  PERFORM FM_APPEND_FIELDCAT USING 'VBELN' '交货单号'.
  PERFORM FM_APPEND_FIELDCAT USING 'POSNR' '交货项次'.
  PERFORM FM_APPEND_FIELDCAT USING 'MATNR' '物料号码'.
  PERFORM FM_APPEND_FIELDCAT USING 'VGBEL' '采购订单'.
  PERFORM FM_APPEND_FIELDCAT USING 'VGPOS' '订单项次'.
  PERFORM FM_APPEND_FIELDCAT USING 'ERDAT' '提单日期'.
  PERFORM FM_APPEND_FIELDCAT USING 'LFIMG' '提单重量'.
  PERFORM FM_APPEND_FIELDCAT USING 'DAYS' '保存天数'.
  PERFORM FM_APPEND_FIELDCAT USING 'CODE' '厂商编码'.
  PERFORM FM_APPEND_FIELDCAT USING 'DOWN' '是否自提'.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_LAYOUT
*&---------------------------------------------------------------------*
*       显示报表结构
*----------------------------------------------------------------------*
FORM FRM_ALV_LAYOUT .
      GW_LAYOUT-CWIDTH_OPT = 'X'.
      GW_LAYOUT-ZEBRA = 'X'.
      GW_LAYOUT-SEL_MODE = 'D'.
      GW_LAYOUT-BOX_FNAME = 'BOX'.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FM_APPEND_FIELDCAT_PRO
*&---------------------------------------------------------------------*
*       定义性能填充模式
*----------------------------------------------------------------------*
FORM FM_APPEND_FIELDCAT  USING   VALUE(FILED_NAME)   VALUE(FILED_TEXT).
  GW_FIELDCAT-FIELDNAME = FILED_NAME.
  GW_FIELDCAT-COLTEXT = FILED_TEXT.
  GW_FIELDCAT-SELTEXT = FILED_TEXT.
  GW_FIELDCAT-TOOLTIP = FILED_TEXT.
  IF GW_FIELDCAT-FIELDNAME = 'MATNR'.
    GW_FIELDCAT-NO_ZERO = 'X'.
  ELSE.
    GW_FIELDCAT-NO_ZERO = ''.
  ENDIF.
  IF GW_FIELDCAT-FIELDNAME = 'ZFMK'.
    GW_FIELDCAT-CHECKBOX = 'X'.
  ELSE.
    GW_FIELDCAT-CHECKBOX = ''.
  ENDIF.
*  IF GW_FIELDCAT-FIELDNAME = 'ZTYPE'."选择对应表数据
*    GW_FIELDCAT-F4AVAILABL = 'X'."输入帮助
*    GW_FIELDCAT-REF_FIELD = 'SDABW'.
*    GW_FIELDCAT-REF_TABLE = 'TVSAKT'.
*  ENDIF.
  IF GW_FIELDCAT-FIELDNAME = 'ERDAT'."选择日期
    GW_FIELDCAT-DATATYPE = 'DATS'.
    GW_FIELDCAT-INTTYPE = 'D'.
    GW_FIELDCAT-REF_FIELD = 'LFDAT'.
    GW_FIELDCAT-REF_TABLE = 'LIKP'.
  ENDIF.
  IF GW_FIELDCAT-FIELDNAME = 'DOWN' .
    GW_FIELDCAT-DRDN_FIELD = 'DD_HANDLE'."
  ELSE.
    GW_FIELDCAT-DRDN_FIELD = ''.
  ENDIF.
  IF GW_FIELDCAT-FIELDNAME = 'LFIMG'.
    GW_FIELDCAT-DATATYPE = 'P'."设置数据类型
  ENDIF.
  IF P_ED = 'X'.
    CASE GW_FIELDCAT-FIELDNAME.
      WHEN 'ERDAT' OR 'LFIMG'  OR 'CODE' OR 'DOWN' OR 'ZFMK'.
        GW_FIELDCAT-EDIT = 'X'.
      WHEN OTHERS.
        GW_FIELDCAT-EDIT = SPACE.
    ENDCASE.
  ENDIF.
  APPEND GW_FIELDCAT TO GT_FIELDCAT.
  CLEAR GW_FIELDCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  SET_STATUS GUI状态设置
*&---------------------------------------------------------------------*
FORM pf_status_set USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD' EXCLUDING rt_extab."显示所有ALV功能按钮
ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  USER_COMMAND  用户自定义按钮
*&---------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING r_ucomm  LIKE sy-ucomm
                         rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN 'ZSAVE'.
      PERFORM frm_save.
  ENDCASE.
  rs_selfield-refresh = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  获取长文本
*&---------------------------------------------------------------------*
FORM FRM_READ_TXT USING FU_ID     TYPE THEAD-TDID
                         FU_NAME   TYPE THEAD-TDNAME
                         FU_OBJECT TYPE THEAD-TDOBJECT
                         FU_LANGU TYPE SY-LANGU
                   CHANGING FU_TEXT.

  DATA: LT_TEXT  TYPE TABLE OF TLINE,
        LT_LINES TYPE TABLE OF TLINE,
        LW_TEXT  LIKE LINE OF LT_TEXT.
  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      CLIENT                  = SY-MANDT
      ID                      = FU_ID
      LANGUAGE                = FU_LANGU
      NAME                    = FU_NAME
      OBJECT                  = FU_OBJECT
      ARCHIVE_HANDLE          = 0
      LOCAL_CAT               = ' '
    TABLES
      LINES                   = LT_TEXT
    EXCEPTIONS
      ID                      = 1
      LANGUAGE                = 2
      NAME                    = 3
      NOT_FOUND               = 4
      OBJECT                  = 5
      REFERENCE_CHECK         = 6
      WRONG_ACCESS_TO_ARCHIVE = 7
      OTHERS                  = 8.
  "将特殊字符正常输出
  CALL FUNCTION 'CONVERT_ITF_TO_STREAM_TEXT'
    EXPORTING
      LANGUAGE    = FU_LANGU
*     LF          = ' '
*     IMPORTING
*     STREAM_LINES       =
    TABLES
      ITF_TEXT    = LT_TEXT
      TEXT_STREAM = LT_LINES.
  IF SY-SUBRC <> 0.
*    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO  "不提示 错误信息
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
*文本拼接
  LOOP AT LT_LINES INTO LW_TEXT.
    CONCATENATE FU_TEXT LW_TEXT INTO FU_TEXT.
    CLEAR: LW_TEXT.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      FORM  FRM_ALV_INIT_SETTINGS
*&---------------------------------------------------------------------*
FORM FRM_ALV_INIT_SETTINGS.
  GS_SETTINGS-EDT_CLL_CB = 'X'."ALV 控制: 退出可编辑单元格时回调
ENDFORM.
FORM CREATE_DROPDOWN_VALUES."设置下拉列表值
  DATA LV_TABIX TYPE I.
  CLEAR:GW_ALV.
  LOOP AT GT_ALV INTO GW_ALV.
    LV_TABIX = SY-TABIX.
    GW_DDVAL-HANDLE = LV_TABIX.
    GW_DDVAL-VALUE = 'Y'.
    APPEND GW_DDVAL TO GT_DDVAL.
    CLEAR GW_DDVAL.
    GW_DDVAL-HANDLE = LV_TABIX.
    GW_DDVAL-VALUE = 'N'.
    APPEND GW_DDVAL TO GT_DDVAL.
    GW_ALV-DD_HANDLE = LV_TABIX.
    MODIFY GT_ALV FROM GW_ALV.
    CLEAR GW_ALV.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SET_ALV_EVENTS
*&---------------------------------------------------------------------*
FORM FRM_SET_ALV_EVENTS CHANGING PT_EVENTS TYPE SLIS_T_EVENT.
  DATA: LS_EVENTS TYPE SLIS_ALV_EVENT.
  LS_EVENTS-NAME = 'CALLER_EXIT'.
  LS_EVENTS-FORM = 'FRM_REGISTER_EVENTS'.
  APPEND LS_EVENTS TO PT_EVENTS.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  register_events
*&---------------------------------------------------------------------*
*       注册回车事件
*----------------------------------------------------------------------
FORM FRM_REGISTER_EVENTS USING E_GRID TYPE SLIS_DATA_CALLER_EXIT.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID = G_GRID.

  CALL METHOD g_grid->SET_DROP_DOWN_TABLE
           EXPORTING
             IT_DROP_DOWN = GT_DDVAL.
*--------------------------------------------------------------------*
*回车更新内表 ,可在对应事件中自定义更新内表动作
  DATA: GR_EVENT_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
  CREATE OBJECT GR_EVENT_HANDLER.
*
  CALL METHOD G_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER
    EXCEPTIONS
      ERROR      = 1
      OTHERS     = 2.

  SET HANDLER GR_EVENT_HANDLER->HANDLE_DATA_CHANGED FOR G_GRID.
*--------------------------------------------------------------------*
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM. "register_events
FORM HANDLE_DATA_CHANGED .
"刷新ALV,优化显示内容
  DATA: ls_stbl TYPE lvc_s_stbl,
        LS_LAYOUT TYPE LVC_S_LAYO.

  CALL METHOD G_GRID->GET_FRONTEND_LAYOUT
    IMPORTING
      es_layout = LS_LAYOUT.

  LS_LAYOUT-CWIDTH_OPT = 'X'.
  CALL METHOD G_GRID->SET_FRONTEND_LAYOUT
    EXPORTING
      IS_LAYOUT = LS_LAYOUT.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SAVE
*&---------------------------------------------------------------------*
FORM FRM_SAVE .
*--------------------------------------------------------------------*
*ALV更新后自动回写对应内表,需设置  edt_cll_cb
  CALL METHOD G_GRID->CHECK_CHANGED_DATA .
  CALL FUNCTION 'POPUP_CONTINUE_YES_NO'
    EXPORTING
      DEFAULTOPTION       = 'Y'
      TEXTLINE1           = '数据已更新'
*     TEXTLINE2           = ' '
      TITEL               = 'Infomation'
*     START_COLUMN        = 25
*     START_ROW           = 6
*   IMPORTING
*     ANSWER              =
            .
*  "锁定 号码范围
*  CALL FUNCTION 'NUMBER_RANGE_ENQUEUE'
*    EXPORTING
*      object           = 'ZCLA'
*    EXCEPTIONS
*      foreign_lock     = 1
*      object_not_found = 2
*      system_failure   = 3
*      OTHERS           = 4.
*  IF sy-subrc <> 0.
*    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*                        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*  ENDIF.
*
*  CALL FUNCTION 'NUMBER_GET_NEXT'
*    EXPORTING
*      nr_range_nr             = '01'
*      object                  = 'ZCLA'
*      toyear                  = sy-datum+0(4)
*    IMPORTING
*      number                  = lv_nr
*    EXCEPTIONS
*      interval_not_found      = 1
*      number_range_not_intern = 2
*      object_not_found        = 3
*      quantity_is_0           = 4
*      quantity_is_not_1       = 5
*      interval_overflow       = 6
*      buffer_overflow         = 7
*      OTHERS                  = 8.
*  IF sy-subrc <> 0.
*    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*                        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*  ENDIF.
*  CONCATENATE 'MC' sy-datum+2(2) lv_nr INTO WA_BOLNR-ZCLANO.
*  CLEAR:WA_BOLNR.
*  "解锁
*  CALL FUNCTION 'NUMBER_RANGE_DEQUEUE'
*    EXPORTING
*      object           = 'ZCLA'
*    EXCEPTIONS
*      object_not_found = 1
*      OTHERS           = 2.
*  IF sy-subrc <> 0.
** Implement suitable error handling here
*  ENDIF.
ENDFORM.

 

标签:GW,SY,&---------------------------------------------------------------------
From: https://www.cnblogs.com/kang2008/p/17776954.html

相关文章

  • 【模板】扩展 kmp (exkmp) / Z 函数
    求出一个字符串\(s\)的每个后缀与原串的LCP。首先由显然的SAM做法。考虑线性。考虑维护区间\([l,r]\)表示\([l,r]=[1,r-l+1]\)是最右的匹配段。考虑新的\(i\),如果满足\(l\leqi\leqr\),则\(i\)可以直接取\(i-l+1\)的答案继续扩展,否则继续扩展。最后更新区间。......
  • 占位符导入模板excel, 再导出xlsx
    1、引入包`<dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version></dependency><dependency><groupId>or......
  • 模板
    模板读写读入快读intread(){ints=0,f=1;charx=getchar();while(x<'0'||'9'<x)f=(x=='-')?-1:1,x=getchar();while('0'<=x&&x<='9')s=s*10+x-......
  • 【模板】二维计算几何初步
    template<classT>structpoint{Tx,y;point():point(0,0){}point(Tx,Ty):x(x),y(y){}friendpointoperator+(constpoint&lhs,constpoint&rhs){return{lhs.x+rhs.x,lhs.y+rhs.y};}friend......
  • 如何使用GraalVM和Java采集天涯图片
    今天我要给大家分享的是如何使用GraalVM和Java编写一个采集天涯论坛图片的程序,内容通俗易懂,非常适合新手学习,大神勿喷。```java//导入必要的库importjava.io.*;importjava.net.*;importjava.util.*;//创建一个名为Downloader的类publicclassDownloader{//定义......
  • C++ 模板特化与偏特化:解析与应用
    引言在C++中,模板是一种非常强大的特性,它们允许我们编写通用、可重用的代码。但有时,我们需要为某些特定的数据类型或类提供特殊的实现,这时就需要使用到模板特化(TemplateSpecialization)和模板偏特化(PartialTemplateSpecialization)。本文将深入探讨这两者的概念、用法和注意事项......
  • Ant Design中表单验证输入框默认值initialValue不更改值会验证不通过(react)
    AntDesign中表单验证输入框默认值initialValue不更改值会验证不通过(react)更改前<Form.Itemlabel="用户标识"name="id"rules={[{required:true,message:'用户标识不能为空!',},]}......
  • GraalVM进行spring boot的native编译报错 “returned non-zero result”解决方法
    使用GraalVM对springboot工程进行native编译:系统:CentosJDK:GraalVMJDK21报错:Ifyouareunabletoresolvethisproblem,pleasefileanissuewiththeerrorreportat:https://graalvm.org/support[INFO]---------------------------------------------------------......
  • C++模板笔记
    参考文章:https://juejin.cn/post/7078530622527897631模板是C++的泛型编程机制,这种机制可以最大程度复用代码并且不会增加运行时开销模板分为函数模板和类模板函数模板函数模板是对函数的参数进行泛型化,传递给模板函数的类型实参可以是类,也可以是整型值,还可以是模板名比如://......
  • 二分模板
    二分答案的写法有很多模板,但使用的情况各不相同前两种模板:都是while(l<r),但是会有区别的,区别在代码注释中有体现。后两种模板:都是while(l<=r),也是在返回上有区别。这是最大值最小intmain(){ intl; intr; while(l<r) { intmid=(l+r)/2; if(check()......