小编Alo*_*mer的帖子

当键值是 std 向量时,为什么在 C++ 中使用 at 访问映射值如此之慢?

我正在使用std::map定义为std::map<std::vector<int>, double>,您会看到键值是整数向量。我的地图中的成员数量是 24600。这是最小的工作示例:

InOutLetFileVelocityWeights.h

#include <iostream>
#include <string>
#include <vector>
#include <map>

class InOutLetFileVelocityWeights
{
        public:
          InOutLetFileVelocityWeights();

          const std::string& GetWeightsFilePath()
          {
            return velocityWeightsFilePath;
          }
          void SetWeightsFilePath(const std::string& path)
          {
            velocityWeightsFilePath = path;
          }

          double GetValue(std::vector<int>& xyz);

          void Initialise();

        private:
          std::string velocityWeightsFilePath;

          std::map<std::vector<int>, double> weights_table;
};
Run Code Online (Sandbox Code Playgroud)

InOutLetFileVelocityWeights.cc

#include "InOutLetFileVelocityWeights.h"
#include <algorithm>
#include <fstream>
#include <cmath>

InOutLetFileVelocityWeights::InOutLetFileVelocityWeights()
{
}

double InOutLetFileVelocityWeights::GetValue(std::vector<int>& xyz)
{

      double value;

      value = weights_table.at(xyz);

      return value;

}

void InOutLetFileVelocityWeights::Initialise()
{
/* Load …
Run Code Online (Sandbox Code Playgroud)

c++ containers stl std c++11

0
推荐指数
1
解决办法
299
查看次数

标签 统计

c++ ×1

c++11 ×1

containers ×1

std ×1

stl ×1