Andrew Truckle
Andrew Truckle

Reputation: 19087

Converting std::vector from BYTE to int

Code:

using ColumnIndexVector = std::vector<int>;
using ByteVector = std::vector<BYTE>;

void CCreateReportDlg::GetColumnIndexesToExclude()
{
    const CString strSection = theApp.GetActiveScheduleSection(_T("Options"));

    ByteVector vData = theApp.GetProfileVector(strSection, _T("AssignStatesEx"));

    ColumnIndexVector vTemp(vData.begin(), vData.end()); // This converts BYTE to int
    m_vColumnIndexesToExclude = vTemp;

}

Is there any way to avoid the requirement for vTemp without manually iterating vData and casting from BYTE to int?

Upvotes: 0

Views: 206

Answers (1)

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

Yes, just use assign(). IDK if you need to use clear() as well, but probably not. Just step through the runtime code the first time to know.

m_vColumnIndexesToExclude.assign(vData.begin(), vData.end());

Here's a test program:

#include <windows.h>
#include <iostream>
#include <vector>

using namespace std;
using ColumnIndexVector = std::vector<int>;
using ByteVector = std::vector<BYTE>;

int main(int argc, char* argv[])
{
    cout << "Test" << endl;
    
    ByteVector bytes = {'A', 'B', 'C', 'D'};
    
    ColumnIndexVector colVector;
    
    for ( auto _val: bytes)
    {
        cout << _val << endl;
    }
    
    colVector.assign(bytes.begin(), bytes.end());
    for ( auto _val : colVector)
    {
        cout << _val << endl;
    }
    
    return 0;
}

Upvotes: 1

Related Questions