Solved

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

Posted on 2001-09-17
7
265 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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