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 8641159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:27:16+00:00 2026-06-12T11:27:16+00:00

An array can be converted into a std::vector easily and efficiently: template <typename T,

  • 0

An array can be converted into a std::vector easily and efficiently:

template <typename T, int N>
vector<T> array_to_vector(T(& a)[N]) {

  return vector<T>(a, a + sizeof(a) / sizeof(T));

}

Is there a similar way to convert a two dimensional array into a std::map without iterating over the members? This looks like an unusual function signature, but in my particular situation, the keys and values in these maps will be of the same type.

template <typename T, int N>
map<T, T> array_to_map(T(& a)[N][2]) {

  // ...?

}

Here’s the test code I put together for this question. It will compile and run as-is; the goal is to get it to compile with the block comment in main uncommented.

#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

template <typename T, int N>
vector<T> array_to_vector(T(& a)[N]) {

  return vector<T>(a, a + sizeof(a) / sizeof(T));

}

template <typename T, int N>
map<T, T> array_to_map(T(& a)[N][2]) {

  // This doesn't work; members won't convert to pair 

  return map<T, T>(a, a + sizeof(a) / sizeof(T));

}

int main() {

  int a[] = { 12, 23, 34 };

  vector<int> v = array_to_vector(a);

  cout << v[1] << endl;

  /*
  string b[][2] = {
    {"one", "check 1"},
    {"two", "check 2"}
   };

  map<string, string> m = array_to_map(b);

  cout << m["two"] << endl;
  */
}

Again, I’m not looking for answers with code that iterates over each member of the array… I could write that myself. If it can’t be done in a better way, I’ll accept that as an answer.

  • 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-12T11:27:17+00:00Added an answer on June 12, 2026 at 11:27 am

    The following works fine for me:

    template <typename T, int N>
    map<T, T> array_to_map(T(& a)[N][2]) 
    {
        map<T, T> result;
        std::transform(
            a, a+N, std::inserter(result, result.begin()),
            [] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); }
            );
    
        return result;
    }
    

    If you have C++03 you could use

    template <typename T>
    static std::pair<T, T> as_pair(T const(&p)[2]) {
        return std::make_pair(p[0], p[1]);
    }
    
    template <typename T, int N>
    map<T, T> array_to_map(T(& a)[N][2]) {
        map<T, T> result;
        std::transform(a, a+N, std::inserter(result, result.begin()), as_pair<T>);
        return result;
    }
    

    Full demo

    Live On Coliru

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    template <typename T, int N>
    vector<T> array_to_vector(T const(& a)[N]) {
      return vector<T>(a, a + sizeof(a) / sizeof(T));
    }
    
    template <typename T>
    static std::pair<T, T> as_pair(T const(&p)[2])
    {
        return std::make_pair(p[0], p[1]);
    }
    
    template <typename T, int N>
    map<T, T> array_to_map(T const(& a)[N][2]) 
    {
        map<T, T> result;
    
        // C++03: std::transform(a, a+N, std::inserter(result, result.begin()), as_pair<T>);
        std::transform(
            a, a+N, std::inserter(result, result.begin()),
            [] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); }
            );
    
        return result;
    }
    
    int main() {
    
      int a[] = { 12, 23, 34 };
      vector<int> v = array_to_vector(a);
      cout << v[1] << endl;
    
      const string b[][2] = {
        {"one", "check 1"},
        {"two", "check 2"}
       };
    
      map<string, string> m = array_to_map(b);
    
      cout << m["two"] << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I converted my arraylist into an array so I can use it to
How can an HTML table be converted into a JavaScript array? <table id=cartGrid> <thead>
Can anyone please tell me how an image(.jpg,.gif,.bmp) is converted into a byte array
how can i convert this into an array? if someone searches for lo he
How can I convert line breaks into codes? For instance, $array = array( 'title'
How can I convert a PSID type into a byte array that contains the
Considering this code: std::vector<myObject*> veryLargeArray; for (int i = 0; i < veryLargeArray.size(); ++i)
How can I convert a string into an array? The values are passed as
#include <iostream> using namespace std; int main(void) { int num[5]; const int* &ref =
How can I access the property/ value of an array which has been 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.