将容器中(数组、集合)的数据串成字符串

本文介绍了一种通用方法,用于将C++ STL容器(如数组、集合等)中的数据转换为字符串形式。该方法利用模板技术实现,并通过具体示例展示了如何根据不同数据类型定制字符串格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发工具VC7(VS2002)
核心代码用到MFC,测试代码用到stl

本代码功能,将容器中(数组、集合)的数据串成字符串

核心代码和扩展代码往往不是一个人完成的。 扩展代码和使用也很可能不是一个人写的。


//核心代码
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(-1.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,-1.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("\r\n"),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("\r\n"),5));
    //AfxMessageBox(str);

    str = STLToStr(a,a+sizeof(a)/sizeof(a[0]),CGetBOOLStr(_T("√"),_T("×")),CGet2ConstSpe(_T(" "),_T("\r\n"),5));
    AfxMessageBox(str);
}
//结果分别为
/*
真 真 假 假 真
真 真
*/
/*
√ √ × × √
√ √
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件架构师何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值