Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:03:34+00:00 2026-06-04T05:03:34+00:00

I have a Visual Studio 2008 C++03 application where I would like to initialize

  • 0

I have a Visual Studio 2008 C++03 application where I would like to initialize a std::vector based on a two dimensional array.

For example:

#define DATA_SIZE 6

struct Data
{
    UINT a;
    BYTE b;
    BYTE c;
    BYTE d;

    Data()
        : /* initialize all members to 0*/
    {
    };

    explicit Data( const BYTE data[ DATA_SIZE ] )
        : a( data[ 0 ] << 16 | data[ 1 ] << 8 | data[ 2 ] ),
          b( data[ 3 ] ),
          c( data[ 4 ] ),
          d( data[ 5 ] )
    {
    };
};

inline bool operator==( const Data& lhs, const Data& rhs )
{
    return /* true if all members are equal */
};

int main( int argc, char* argv[] )
{
    const BYTE source[][ DATA_SIZE ] = 
    {
        { 0x01, 0xfe, 0xaa, 0x01, 0xcc, 0x13 },
        { 0x02, 0xa1, 0x02, 0xbb, 0x02, 0xdd }
    }

    // how should this be done?
    std::vector< Data > data_list( source[ 0 ], source[ countof( source) - 1 ] );

    ASSERT( data_list[ 0 ] == Data( source[ 0 ] ) );
    ASSERT( data_list[ 1 ] == Data( source[ 1 ] ) );
    return 0;
}

Is there a way to do this without a for loop iterating through each item in the data array and calling push_back?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:03:36+00:00Added an answer on June 4, 2026 at 5:03 am

    Here’s an approach using Boost.Array:

    #include <cstddef>
    #include <cassert>
    #include <vector>
    #include <boost/array.hpp>
    
    typedef unsigned UINT;
    typedef unsigned char BYTE;
    
    std::size_t const DATA_SIZE = 6;
    
    struct Data
    {
        UINT a;
        BYTE b, c, d;
    
        Data() : a(), b(), c(), d() { }
    
        Data(boost::array<BYTE, DATA_SIZE> const& data)
          : a(data[0] << 16 | data[1] << 8 | data[2]),
            b(data[3]),
            c(data[4]),
            d(data[5])
        { }
    };
    
    inline bool operator ==(Data const& lhs, Data const& rhs)
    {
        return lhs.a == rhs.a
            && lhs.b == rhs.b
            && lhs.c == rhs.c
            && lhs.d == rhs.d;
    }
    
    int main()
    {
        boost::array<boost::array<BYTE, DATA_SIZE>, 2> const source =
        {{
            {{ 0x01, 0xfe, 0xaa, 0x01, 0xcc, 0x13 }},
            {{ 0x02, 0xa1, 0x02, 0xbb, 0x02, 0xdd }}
        }};
    
        std::vector<Data> data_list(source.begin(), source.end());
    
        assert(data_list[0] == Data(source[0]));
        assert(data_list[1] == Data(source[1]));
    }
    

    Online demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Visual Studio 2008 C++ application where I would like to copy
I have a Visual Studio 2008 C++ application where I would like to convert
I have a Visual Studio 2008 C++ application where I would like to replace
I have a Visual Studio 2008 C++ application where I would like to insert
I have a Visual Studio 2008 C++03 application where I would like to use
I have a Visual Studio 2008 C++ application that does something like this: template<
I have a Visual Studio 2008 C++ application for Windows 7 where I would
I have a Visual Studio 2008 C++03 application where I have two standard containers.
I have application written in Visual Studio 2008 which I deploy with ClickOnce to
I have a GUI C++ application (Visual Studio 2008) that needs to be converted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.