Copy the contents of CStringArray to std::vector

c++, mfc, stl

Solution

#include <string>
#include <vector>

#if defined(UNICODE) || defined(_UNICODE)
typedef std::wstring string;
#else
typedef std::string string;
#endif

typedef std::vector<string> StringVector;

void CmfcstrarDlg::OnBnClickedButton1()
{

    CStringArray strs;
    strs.Add(_T("one"));
    strs.Add(_T("two"));
    strs.Add(_T("three"));

    StringVector copy;

    for (int n = 0; n < strs.GetCount(); n++)
    {
        const CString& s = strs.GetAt(n);
        copy.push_back(string(s));
    }

    StringVector::const_iterator citer = copy.cbegin();
    for (; citer != copy.cend(); citer++)
    {
        OutputDebugString(citer->c_str());
        OutputDebugString(_T("\n"));
    }

}

Problem

As the question states I would like to copy the contents of a `CStringArray` into a `std::vector<std::string>`. Any suggestions?

Original source