Solved

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

Posted on 2001-09-17
7
225 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
  • 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now