将容器中(数组、集合)的数据串成字符串
来源:程序员人生 发布时间:2015-01-22 08:33:49 阅读次数:3054次
开发工具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);
}
//结果分别为
/*
真 真 假 假 真
真 真
*/
/*
√ √ × × √
√ √
*/
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠