日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > 框架設計 > 將容器中(數組、集合)的數據串成字符串

將容器中(數組、集合)的數據串成字符串

來源:程序員人生   發布時間:2015-01-22 08:33:49 閱讀次數:3292次
開發工具VC7(VS2002)
核心代碼用到MFC,測試代碼用到stl

本代碼功能,將容器中(數組、集合)的數據串成字符串

核心代碼和擴大代碼常常不是1個人完成的。 擴大代碼和使用也極可能不是1個人寫的。


//核心代碼
template<class TYPE>
class IGetEleStr
{
public:
    virtual  CString GetStr(const TYPE& ele) const =0 ;
};


class IGetSpe
{
public:
    virtual CString GetSpe(int index) const = 0;
};


template<class EleTypePtr,class EleType>//如果是stl的迭代器,EleTypePtr不等于EleType*
CString STLToStr(EleTypePtr begin,EleTypePtr end, const IGetEleStr<EleType>& ele, const IGetSpe& spe )
{
    CString str ;
    int index = 0 ;//其實不是所有的迭代器都支持相減
    for( EleTypePtr it = begin ; it != end ; ++ it )
    {
        if( 0 != index )
            str += spe.GetSpe(index);
        index++;
        str += ele.GetStr(*it);
    }
    return str;
}

//擴大代碼
class CGetConstSpe : public IGetSpe
{
public:
    CGetConstSpe(const CString strSpe)
    {
        m_strSpe = strSpe;
    }
    virtual CString GetSpe(int index) const
    {
        return m_strSpe;
    }
protected:
    CString m_strSpe;
};

template<class EleType>
class  CGetFormatStr : public IGetEleStr<EleType>
{
public:
    CGetFormatStr(const CString& strFormat)
    {
        m_strFormat = strFormat;
    }
    virtual  CString GetStr(const EleType& ele) const
    {
        CString str;
        str.Format(m_strFormat,ele);
        return str;
    }
protected:
    CString m_strFormat;
};

//測試代碼
#include "afxtempl.h"
#include <set>

void Ctest1Dlg::OnBnClickedButton11()
{
    int a[] = {3,4,5,6,7,8};
    CString str = STLToStr(a,a+sizeof(a)/sizeof(a[0]),CGetFormatStr<int>(_T("%d")),CGetConstSpe(_T(" ")));
    AfxMessageBox(str);

//    CArray<float,float> f ;
    f.Add(3.3);
    f.Add(1.02);
    f.Add(0);
    f.Add(⑴.33333334);
    str = STLToStr(f.GetData(),f.GetData()+f.GetSize(),CGetFormatStr<float>(_T("%3.1f")),CGetConstSpe(_T(",")));
//    AfxMessageBox(str);

    std::set<CString> sets;
    sets.insert(CString(_T("a")));
    sets.insert(CString(_T("c")));
    str = STLToStr(sets.begin(),sets.end(),CGetFormatStr<CString>(_T("'%s'")),CGetConstSpe(_T(",")));
    AfxMessageBox(str);
}
//結果分別為:
//3 4 5 6 7 8
//3.3,1.0,0.0,⑴.3
//'a','c'

class CGet2ConstSpe : public IGetSpe//索引+1為index的倍數,則用strSpe分隔,否則用strNormalSpe
{
public:
    CGet2ConstSpe(const CString strNormalSpe,const CString strSpe,int index)
    {
        m_strNormalSpe = strNormalSpe;
        m_strSpe = strSpe;
        m_index = index ;
    }
    virtual CString GetSpe(int index) const
    {
        if( ( m_index > 0 ) && ( index % m_index == 0 ) )
            return m_strSpe;
        return m_strNormalSpe;
    }
protected:
    CString m_strSpe;
    CString m_strNormalSpe;
    int m_index;
};

void Ctest1Dlg::OnBnClickedButton12()
{
    int a[] = {3,4,5,6,7,8};
    CString str = STLToStr(a,a+sizeof(a)/sizeof(a[0]),CGetFormatStr<int>(_T("%d")),CGet2ConstSpe(_T(" "),_T(" "),5));
    AfxMessageBox(str);
}
/*  運行結果
3 4 5 6 7
8
*/



class  CGetBOOLStr : public IGetEleStr<BOOL>
{
public:
    CGetBOOLStr(const CString& strTrue=_T("真"),const CString& strFalse=_T("假"))
    {
        m_strTure = strTrue;
        m_strFalse = strFalse;
    }
    virtual  CString GetStr(const BOOL& ele) const
    {
        return (ele) ? m_strTure : m_strFalse ;
    }
protected:
    CString m_strTure;
    CString m_strFalse;
};

void Ctest1Dlg::OnBnClickedButton13()
{
    BOOL a[] = {TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE};
    CString str = STLToStr(a,a+sizeof(a)/sizeof(a[0]),CGetBOOLStr(),CGet2ConstSpe(_T(" "),_T(" "),5));
    //AfxMessageBox(str);

    str = STLToStr(a,a+sizeof(a)/sizeof(a[0]),CGetBOOLStr(_T("√"),_T("×")),CGet2ConstSpe(_T(" "),_T(" "),5));
    AfxMessageBox(str);
}
//結果分別為
/*
真 真 假 假 真
真 真
*/
/*
√ √ × × √
√ √
*/
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美日韩一区视频 | 这里只有精品免费视频 | 精品毛片 | 亚洲欧美日韩另类 | 欧美日视频 | 黄视频网站在线观看 | 成人精品视频 | www.色综合| 亚洲经典自拍 | 亚洲不卡一区二区三区 | 操操综合 | 欧日韩在线观看 | 黄色一几片| 久久好色 | 久久久久久亚洲 | 亚洲午夜精品 | 国产精品久久福利 | 中文字幕不卡av | 天堂网久久 | 久久九色 | 日本大黄视频 | 成人免费观看在线 | 91福利视频合集 | 久热中文| 天堂男人av | 国产精品久久久av | 欧美日韩激情在线一区二区三区 | 国产日韩欧美一二三区 | 日本色一区二区 | 7777久久香蕉成人影院 | 欧美精品第一页 | 永久免费av网站 | 国产精品资源 | 久久精品国产99国产 | 成人免费网站在线观看 | 国产精品久久久久久久久久 | 精品久久一区二区 | 午夜三级在线观看 | 夜夜艹 | 色婷婷成人影院 | 成年人在线看片 |