Solved

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

Posted on 2001-09-17
7
256 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 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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