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
Solved

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

Posted on 2001-09-17
7
251 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 107
Would like to move button in a function 3 79
How to gracefully close the c++ 11 thread? 3 105
Outlook 13 39
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

791 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