闪亮的马路 发表于 2021-10-29 13:42:24

关于嵌套的map的问题

如下面的程序,
frame_i我理解的是一个迭代,所以frame->不是应该是指向的ImageFrame实例化的all_image_frame吗?
然后points是其中的一个map映射,问两个问题
(1)怎么通过frame_i访问其中的points,t,R,T?
(2)frame_i->second.R为什么就直接可以指到类中的R呢?


void solveGyroscopeBias(map<double, ImageFrame> &all_image_frame, Vector3d* Bgs)
{
    Matrix3d A;
    Vector3d b;
    Vector3d delta_bg;
    A.setZero();
    b.setZero();
    map<double, ImageFrame>::iterator frame_i;
    map<double, ImageFrame>::iterator frame_j;
    for (frame_i = all_image_frame.begin(); next(frame_i) != all_image_frame.end(); frame_i++)
    {
      frame_j = next(frame_i);
      MatrixXd tmp_A(3, 3);
      tmp_A.setZero();
      VectorXd tmp_b(3);
      tmp_b.setZero();
      Eigen::Quaterniond q_ij(frame_i->second.R.transpose() * frame_j->second.R);
      tmp_A = frame_j->second.pre_integration->jacobian.template block<3, 3>(O_R, O_BG);
      tmp_b = 2 * (frame_j->second.pre_integration->delta_q.inverse() * q_ij).vec();
      A += tmp_A.transpose() * tmp_A;
      b += tmp_A.transpose() * tmp_b;

    }
    delta_bg = A.ldlt().solve(b);
    // ROS_WARN_STREAM("gyroscope bias initial calibration " << delta_bg.transpose());

    for (int i = 0; i <= WINDOW_SIZE; i++)
      Bgs += delta_bg;

    for (frame_i = all_image_frame.begin(); next(frame_i) != all_image_frame.end( ); frame_i++)
    {
      frame_j = next(frame_i);
      frame_j->second.pre_integration->repropagate(Vector3d::Zero(), Bgs);
    }
}
//
class ImageFrame
{
public:
    ImageFrame(){};
    ImageFrame(const map<int, vector<pair<int, Eigen::Matrix<double, 7, 1>>>> &_points, double _t) : t{_t}, is_key_frame{false}
    {
      points = _points;
    };
    map<int, vector<pair<int, Eigen::Matrix<double, 7, 1>>>> points;
    double t;
    Matrix3d R;
    Vector3d T;
    IntegrationBase *pre_integration;
    bool is_key_frame;
};

yuxijian2020 发表于 2021-10-29 16:58:14

首先c++里的map很坑   它的键值 分别叫做 first secondfirst 是键   second 是值
frame_i 是一个 迭代器(iterator)迭代器一般都会重载 '->' 符号让它可以直接访问键值

frame_i->second.R这里就按上面的理解   frame_i 是 all_image_frame 的迭代器
所以 frame_i->second 就是访问 all_image_frame 中当前迭代器所在位置的值 也就是 ImageFrame 对象
然后 ImageFrame 类所有属性和方法都是公开的所以通过 '.'(点) 符号就可以直接访问
页: [1]
查看完整版本: 关于嵌套的map的问题