?
Solved

How can I get the memory size from a stl map container?

Posted on 2001-09-17
7
Medium Priority
?
266 Views
Last Modified: 2012-06-27
friends, please help me on the following :-

#include <map>
#include <string>
using namespace std;

typedef map<string, int> STR_MAP;

int main(int argc, char* argv[])
{
  STR_MAP strmap;
  for(int i=0; i <1000; i++) {
    strmap.insert(STR_MAP::value_type(string  
               ("something"), i));
        }

/// i would like to get the memory allocated size of this
// map container
   // mem_size(strmap) ????????/


}
0
Comment
Question by:pphari
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6489102
Because std::string does not have a fix size, you would have to iterate through the list, to get the complete size.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6489122
You can use the following method to get the accumulate() function to get the total size:

Example code:

#include <map>
#include <string>
#include <numeric>

using namespace std;

typedef map<string, int> STR_MAP;

int GetTotalSize(int Accum, pair<string, int> Cur)
{
     return Accum + Cur.first.size() + sizeof(Cur.second);
}

int main(int argc, char* argv[])
{
     STR_MAP strmap;
     for(int i=0; i <1000; i++)
     {
          strmap.insert(STR_MAP::value_type(string  
               ("something"), i));
     }

     int TotalStringSize = accumulate(strmap.begin(),strmap.end(),0,GetTotalSize);
     
     return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 6489125
The above code will return a number of 13, because you only have one item in the container.
Even though you add this (string("something"), i) to the container a 1000 times, you still only end up with one item because the key is the string.  Since the string remains the same, you're inserting the item in the same location over and over again.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Accepted Solution

by:
Axter earned 400 total points
ID: 6489137
In the following example, I reversed the map pair to <int, string>.
This allows your code to insert 1000 items.

#include <map>
#include <string>
#include <numeric>

using namespace std;

typedef map<int, string> STR_MAP;

int GetTotalSize(int Accum, pair<int, string> Cur)
{
     return Accum + sizeof(Cur.first) + Cur.second.size();
}

int main(int argc, char* argv[])
{
     STR_MAP strmap;
     for(int i=0; i <1000; i++)
     {
          strmap.insert(STR_MAP::value_type(i, string ("something")));
     }
     
     int TotalSizeOfAllItemsInContainer = accumulate(strmap.begin(),strmap.end(),0,GetTotalSize);
     int TotalQtyOfPairsInContainer = strmap.size();
     int TotalSizeOfPair = sizeof(pair<int, string>);
     
     int TotalSizeOfContainer = TotalSizeOfAllItemsInContainer +
                    (TotalQtyOfPairsInContainer * TotalSizeOfPair) +
                    sizeof(STR_MAP);
     return 0;
}

0
 

Author Comment

by:pphari
ID: 6489503
hi axter, thanks for your great answer, infact my hash key is string only, the value of its changes all the time and container is a multimap..
once again thanks...
Hari
0
 

Author Comment

by:pphari
ID: 6489504
hi axter, thanks for your great answer, infact my hash key is string only, the value of its changes all the time and container is a multimap..
once again thanks...
Hari
0
 

Author Comment

by:pphari
ID: 6489505
hi axter, thanks for your great answer, infact my hash key is string only, the value of its changes all the time and container is a multimap..
once again thanks...
Hari
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question