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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:17:14+00:00 2026-06-05T08:17:14+00:00

Just as we can initialize vectors as: vector<int> var1(2000,1); Is it possible to initialize

  • 0

Just as we can initialize vectors as:

  vector<int> var1(2000,1);

Is it possible to initialize map;

  map<int, int>var2;

for 2000 variables…the reason why I want to initialize are two:

  1. In case I access an element in future e.g. map[100]..I want that map[100]=0
  2. The second reason is that I am using a minimum priority queue which for comparison uses the second value of map i.e. the value stored in map[0]…map[100].
  3. I don’t want to use vectors as my indices are really skewed and this leads to a lot of wasted space…i.e. my indices are map[0], map[30], map[56],map[100],map[120],map[190], etc.

Is there some way by which I can initialize the map for say 1000 variables…I am also open to using any other data structure.

Also the conventional way of initializing map i.e.

  map<int, int> m = map_list_of (1,2) (3,4) (5,6) (7,8);

The above way does not work in my case…is there some other way out.PLEASE HELP

EDIT: I can not use for loop as:

This way the key remains fixed which I don’t want since the distribution of my keys is skewed. In essence applying for loop in this way is the same as that of vector and this I don’t want

  • 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-05T08:17:15+00:00Added an answer on June 5, 2026 at 8:17 am

    You can do it using a surrogate instead of an int in your map, like this:

    #include <iostream>
    #include <map>
    
    using namespace std;
    
    struct surrogate_int {
        int val;
        surrogate_int() : val(1) {}
        surrogate_int& operator=(const int v) { val=v; }
        operator const int() { return val; }
    };
    
    int main() {
    map<int,surrogate_int> m;
        m[5] = 5;
        m[7] = 7;
        m[9] = 9;
        for (int i = 0 ; i != 10 ; i++) {
            cout << i << ":" << m[i] << endl;
        }
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just can't find a way to transform an Hex String to a number (Int,
Can I initialize a vector<bool> to start with a sequence of 010101...etc? Or do
Would it be possible to initialize a vector array of strings? for example: static
You can initialize an array like this: int [ ] arr = { 1,
I can initialize the list of Int such beautiful way: [2, 4 .. 20]
Just can't seem to figure this out, how to prevent the outer loop continuing
I just can't get to set sessions in php, and I can't find the
I just can't figure it out.. but i got a feeling the problem is
I just can't get Multi Value settings to work in my Ipad app. This
I just can't figure out why i get the error message, I have tried

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.