Concatenates data in containers (arrays and collections) into strings.

Source: Internet
Author: User

Concatenates data in containers (arrays and collections) into strings.
Development Tool VC7 (VS2002)
The core code uses MFC and the test code uses stl.

This code concatenates data in containers (arrays and collections) into strings.

Core code and extension code are often not completed by one person. The extension code and usage may not be written by one person.


// Core code
Template
Class IGetEleStr
{
Public:
Virtual CString GetStr (const TYPE & ele) const = 0;
};


Class IGetSpe
{
Public:
Virtual CString GetSpe (int index) const = 0;
};


Template // If it is an stl iterator, EleTypePtr is not equal to EleType *
CString STLToStr (EleTypePtr begin, EleTypePtr end, const IGetEleStr & Ele, const IGetSpe & spe)
{
CString str;
Int index = 0; // not all iterators support subtraction.
For (EleTypePtr it = begin; it! = End; ++ it)
{
If (0! = Index)
Str + = spe. GetSpe (index );
Index ++;
Str + = ele. GetStr (* it );
}
Return str;
}

// Extended code
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 CGetFormatStr: public IGetEleStr
{
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;
};

// Test code
# Include "afxtempl. h"
# Include

Void Ctest1Dlg: OnBnClickedButton11 ()
{
Int a [] = {3, 4, 5, 6, 7, 8 };
CString str = STLToStr (a, a + sizeof (a)/sizeof (a [0]), CGetFormatStr (_ T ("% d"), CGetConstSpe (_ T ("")));
AfxMessageBox (str );

// CArray F;
F. Add (1, 3.3 );
F. Add (1, 1.02 );
F. Add (0 );
F. Add (-1.33333334 );
Str = STLToStr (f. GetData (), f. GetData () + f. GetSize (), CGetFormatStr (_ T ("% 3.1f"), CGetConstSpe (_ T (",")));
// AfxMessageBox (str );

Std: set Sets;
Sets. insert (CString (_ T ("")));
Sets. insert (CString (_ T ("c ")));
Str = STLToStr (sets. begin (), sets. end (), CGetFormatStr (_ T ("'% S'"), CGetConstSpe (_ T (",")));
AfxMessageBox (str );
}
// The results are as follows:
// 3 4 5 6 7 8
// 3.3, 1.0, 0.0,-1.3
// 'A', 'C'

Class CGet2ConstSpe: public IGetSpe // The index + 1 is a multiple of the indexes, which are separated by strSpe; otherwise, strNormalSpe is used.
{
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 (_ T ("% d"), CGet2ConstSpe (_ T (""), _ T ("\ r \ n"), 5 ));
AfxMessageBox (str );
}
/* Running result
3 4 5 6 7
8
*/



Class CGetBOOLStr: public IGetEleStr
{
Public:
CGetBOOLStr (const CString & strTrue = _ T ("true"), const CString & strFalse = _ T ("false "))
{
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 };
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 );
}
// The result is
/*
True or false
Zhenzhen
*/
/*
√ ××√
√
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.