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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:44:43+00:00 2026-06-05T12:44:43+00:00

I would like to write some class which maps int to something using templates.

  • 0

I would like to write some class which maps int to something using templates.
What I’m thinking of are generally two options:

1. unsigned int ->  double          (scalar)
2. unsigned int ->  double[N]       (vector of length N; N is the same for each int)

I write a class around

template <class T>
class int2type_storage {
  public:
    ....
  private:
    typename std::map<unsigned int,T> map_;
}

With the first case, the usage is straightforward:

int2type_storage<double> map1;

The question is, what is the most efficient way/object for the second case?
I was thinking to do something like

 int2type_storage< std::vector<double> >

but I have a feeling that this will be sub-optimal. Another option is to store pointers

 int2type_storage< double* >

but then I have a problem that I should allocate memory for N elements outside of the map-class and take care to free it later.

EDIT1: Thank you guys for answering, I feel sorry that I can’t mark two answers as correct.

EDIT2:

I have implemented everything, but my linker could not find functions:

undefined reference to `int2type_storage<std::tr1::array<double, 4ul> >::init(int, int)'

.h:

template <class T>
class int2type_storage {
public:
    int2type_storage() {};
    ~int2type_storage() {};

    void init(const int number, const int index);
        ...
private:
    int cur_index_;
    typename std::map<unsigned int, T>::iterator iterator_;
    typename std::vector<std::map<unsigned int,T> > map_vector_;
    bool zero_initialized;
};

.cpp:

template<class T>
void int2type_storage< T >::init(const int length, const int max_index) {
    map_vector_.resize(length);
}

usage:

int2type_storage< std::tr1::array<double, 4> > w_map_;

what’s wrong?

  • 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-05T12:44:45+00:00Added an answer on June 5, 2026 at 12:44 pm

    Assumming N is known at compile-time, you could use an std::array<double,N>:

    int2type_storage< std::array<double, N> >
    

    I am not sure what the reasons for the int2type_storage wrapper are, but you could also use a C++11 template typedef:

    template <typename T, int N>
    using int2type_storage = std::map<unsigned int, std::array<T,N>>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have some functionality by which if I write <textarea maxlength=50></textarea>
I started to write a class which would act much like the std::vector but
I would like to write some test code for a C++ class. Because the
I would like to write some Objective-C code for distribution. What is the best
I would like to write a command line tool that passes some formatted text
I'd like to write a function that would have some optional code to be
I would like to write an if statement that will do something base on
I have some objects which have been created automatically by linq2SQL. I would like
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self):
I'd like to write some code which looks at a website and its assets

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.