首页 > 其他分享 >将容器中(数组、集合)的数据串成字符串

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

时间:2022-10-05 11:01:18浏览次数:56  
标签:容器 串成 index CString str 字符串 const public strSpe

开发工具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);

}

//结果分别为

/*

真 真 假 假 真

真 真

*/

/*

√ √ × × √

√ √

*/

标签:容器,串成,index,CString,str,字符串,const,public,strSpe
From: https://blog.51cto.com/u_15724537/5732417

相关文章