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

  • Home
  • SEARCH
  • 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 7575607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:43:24+00:00 2026-05-30T16:43:24+00:00

I have a series of several similar structures, e.g., map<key1, attr1>; map<key2, attr2>; …,

  • 0

I have a series of several similar structures, e.g.,

map<key1, attr1>; map<key2, attr2>; ..., 

meanwhile, in correspondence, a series of array,

// JUST TO SHOW THE IDEA, NOT SYNTAX CORRECT
class {key_1; attr_1;} array [#]; class {key_2; attr_2;} array [#]; ...,

and keyX and key_X share the similar structure, as well as attr, e.g.

struct key1 {int k1;}; class key_1 {int k_1;};
struct key2 {int k1; int k2;}; class key_2 {int k_1; int k_2;};
   ...
struct attr1 {int a1; int a2;}; class attr_1 {int a_1; int a_2;};

I need to write a function, like overloading the assignment operator, to convert the data from map_series to the other, and vice versa.

So instead of doing it map by map, key by key, and int by int, is there some template-wise way to do it, so the code would be generic,or save more lines of code? What could be the smart way to do this?

EDIT 1:

Unfortunately, one constraint due to our legacy system, the type convertion between struct, e.g. key1, key_1, does not work as C++ primitives, so a conversion function also have to be provided.

EDIT 2:

inspired by J.N. answer, is it possible to have something like:

template <class KeyMap, class ValueMap, class KeyArr, class ValueArr> void convert (map<KeyMap, ValueMap>, class {KeyArr, ValueArr} array[]){};

how to generlize the conversion for key and attr? like

template <class KeyMap, class KeyArr> void convert_key(KeyMap, KeyArr){} 
  • 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-05-30T16:43:25+00:00Added an answer on May 30, 2026 at 4:43 pm

    Ok so let’s suppose we have this:

    map<KeyTypeM, ValueTypeM> m1;
    struct Type1 {
        KeyTypeS Key;
        ValueTypeS Value;
    };
    

    Let’s define the conversion function first:

    Type1 ConvType1(const KeyTypeM& key, const ValueTypeM& value)
    {
        Type1 result;
        result.Key = f(key); // user dependent
        result.Value = f(value); // user dependent
    
        return result;
    }
    

    Then, in C++, you can’t return arrays. Actually you usually don’t work with array, people usually prefer std::vector. (If you really need an array then you need to allocate it and return a pointer to it or wrap it in a smart pointer)
    Then it is enough to browse the values in the map and push them into the vector:

    vector<Type1> ConvertType1(map<KeyTypeM, ValueTypeM>& input)
    {
        vector<Type1> result;
        for (auto& pair : input) // assumes C++11
            result.push_back( ConvType1(pair.first, pair.second) );
        return result;
    }
    

    note: if you do not have a C++11 compiler, use:

    for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it)
        result.push_back( ConvType1(it->first, it->second) );
    

    That will do the job for ONE map and one type. Now we can generalize the conversion function by using templates:

    template <class OutType, class KeyType, class ValueType, class Converter>
    vector<OutType> Convert(map<KeyType, ValueType>& input, Converter conv)
    {
        vector<OutType> result;
        for (auto& pair : input)
            result.push_back( conv(pair.first, pair.second) );
        return result;
    }
    

    And use it like this:

    auto v1 = Convert<Type1>(m1, &ConvertType1);
    auto v2 = Convert<Type2>(m2, &ConvertType2);
    ...
    

    You could go further by creating a list of types to convert using boost::mpl but that should be in another question.

    EDIT: generalize the conversion of the types

    As you mentioned, there’s no generic way to convert the types, we’ll have to write all the conversion functions. So the only thing we can do is make it more elegant by implementing implicit conversion operators like this:

    struct KeyTypeM
    {
         ... // normal members
         operator KeyTypeS() const
         {
             // do the conversion here
         }
    };
    
     // suppose we have the same for ValueTypeM and ValueTypeS
    
     // We can now use a single convert function:
    
     template <class OutType, class KeyTypeM, class ValueType M>
     OutType ConvertToStruct(const KeyTypeM& key, const ValueTypeM& value)
     {
          OutType result;
          result.Key = key;      // will call the implicit conversion
          result.Value = value;
          return result;
     }
    

    This simplifies our conversion function (updated to take into account the array)

    template <class OutType, class KeyType, class ValueType>
    void Convert(map<KeyType, ValueType>& input, OutType out[])
    {
        // out must have been initialized to a proper size to hold all the elements.
        unsigned cursor = 0;
        for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it, ++cursor)
            out[cursor] = ConvertToStruct<OutType>(it->first, it->second);
    }
    

    EDIT 2: We can use std::pair to generalize the array structure:

     template <class OutType, class KeyTypeM, class ValueType M>
     OutType ConvertToStruct(const KeyTypeM& key, const ValueTypeM& value)
     {
          OutType result;
          result.first = key;      // will call the implicit conversion
          result.second = value;
          return result;
     }
    
    template <class OutKey, class OutValue, class KeyType, class ValueType>
    void Convert(map<KeyType, ValueType>& input, std::pair<OutKey, OutValue> out[])
    {
        // out must have been initialized to a proper size to hold all the elements.
        unsigned cursor = 0;
        for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it, ++cursor)
            out[cursor] = ConvertToStruct<OutType>(it->first, it->second);
    }
    
     // Use this way:
    
     std::pair<OutKey, OutValue> arr[m.size()];
     Convert(m, arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several time series plotted with jfreechart. The graph also contains multiple annotations.
I have a subclassed UIView (currentMapView) that draws a map of several states using
I have a series of MovieClips, held within several MovieClips, that dispatch a custom
I have several data series plotted on a chart. This is logged data spanning
i have a series of links to specific addresses on google maps. several of
I have Chart1[Series1] Chart1[Series2] .... It has multiple Series and each series has Several
I have several time series, which when I plot using, plot(na.omit(d)) I get the
I have a series of tables which I want to all have an order
I have a series of strings, which are two numbers: 1.782-100.799 -18.107-102.016 -17.504104.059 How
I have a series of JCheckBoxes(1-20) and each is associated with a JTextField(1-20). I

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.