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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:55:25+00:00 2026-05-25T09:55:25+00:00

I have to store integer values which are mapped to other integers. One way

  • 0

I have to store integer values which are mapped to other integers. One way to do it is use a std::map m. But then to retrieve the value using m[int] or m.find(int), will be an order of logN (N is number of elements) time. In my case N is pretty large (upto 2^30). I thought using a std::vector will be faster for access as each key now maps to index of a vector element, which can be accessed in O(1) time. The key is encountered in random order and they may not be contiguous. For e.g., if the vector has size 10, then not all 10 elements are valid and there could only be 6 valid elements and rest I need to fill with -1. I wrote a small program and I’m surprised to find output below:

int main () {
   std::vector<int> v;
   v.assign(6, -1); 
   v[3] = 10; 
   v[10] = 100;
   cout << v.size() << v.capacity() << endl ;
   cout << v[3] << v[10] << endl; 
}

The output seen is size = 6, capacity = 6, V[3] = 10, v[10] = 100. I do not understand how size and capacity are 6, but v[10] has a valid value or I did not encounter a seg. fault. Can someone explain this? My understanding is push_back function dynamically resizes the vector when vector.size > vector.capacity, does operator [] also do this? To be safe, I re-wrote the above code as :

int main () {
   std::vector<int> v;
   v.assign(6, -1);
   int key = getKey(); 
   if (key < v.size()) 
      v[key] = <correct value>;
   else {
      v.resize(key, -1); // I want to assign -1 to invalid elements
      v[key-1] =  <correct value>;
   }
}

It seems to be working fine, but will it be better to compare key with v.capacity() and then resize the vector.

  • 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-25T09:55:26+00:00Added an answer on May 25, 2026 at 9:55 am
    std::vector<int> v;
    v.assign(6, -1); 
    v[3] = 10; 
    v[10] = 100;  // Unlucky with this statement
    

    The size of the vector is just 6. And the accessing index is from 0 to 5. Your first snippet has undefined behavior that you got unlucky that it didn’t break when accessing an index 10. If there is a notion a key association with a value, then associative containers like std::map or std::multimap would be better than a sequence container like std::vector.

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

Sidebar

Related Questions

I have 2 arraylists. One is principleList which contains integer values that denotes roles
I have a database table which contains an unsigned integer field to store the
i have two List A,B which consists integer values ,list A contains 40 to
I have a table which will potentially store hundreds of thousands of integers: desc
I have created a hash map in which each entry corresponds to 3 values
If I have a simple piece of data to store (an integer or string
I have an C# dll project for which I have to store the runtime
I have an ActiveRecord model Media which is supposed to be able to store
I have a table. The table needs to store a number values about a
Any idea what is the best way to store Twitter's tweet ID (and other

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.