c++:查找程序中的最大容器

标签 c++ size containers analysis

我正在尝试分析一个大型 C++ 程序。该程序大量使用 STL 容器数据结构,如集合、映射、无序集合、无序映射、 vector 等。有时它们是嵌套的,例如集合图。

我想找出,在程序的特定运行中,哪些容器包含最多的元素(即 size() 的最大值)。我可以对程序进行微小的修改。

如果有一种方法可以遍历所有容器,或者如果有一种方法可以拦截容器的(大小修改)API,那可能会有所帮助。但这些都是不可能的。

你会如何处理这个问题?

补充:Linux平台,编译器为g++或clang++。

最佳答案

如果你可以做一些小的编辑,你能把每个容器都添加到一个大列表中吗?
像这样:

std::set<......> my_set;  // existing code
all_containers.add( &my_set ); // minor edit IMHO

然后您可以调用 all_containers.analyse(),它会在每个容器上调用 size() 并打印结果。

你可以使用类似的东西:

struct ContainerStatsI {
  virtual int getSize() = 0;
};
template<class T> struct ContainerStats : ContainerStatsI {
  T* p_;
  ContainerStats( T* p ) : p_(p) {}
  int getSize() { return p->size(); }
};
struct ContainerStatsList {
  std::list<ContainerStatsI*> list_; // or some other container....
  template<class T> void add( T* p ) {
    list_.push_back( new ContainerStats<T>(p) );
  }
  // you should probably add a remove(T* p) as well
  void analyse() {
    for( ContainerStatsI* p : list_ ) {
      p->getSize(); // do something with what's returned here
    }
  }
};

关于c++:查找程序中的最大容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436412/

相关文章:

c++ - 二叉树中的最小路径和

c++ - 无法打开使用 ofstream 创建的文件

html - 无需下载即可获取页面大小

用于以字节为单位返回文件大小的 VBA Excel 函数

c++ - 是否有任何序列容器也维护主键? (c++)

c++ - C++ 模板的替代品?

c++ - memcpy 出错,长度不正确

swift - UIScreen.main.bounds.width 在 Swift 3 中返回 nil

jenkins - 以 root 用户身份访问 pod 的远程 shell

Docker 查看失败/崩溃容器的日志