如何通过at()函数访问2D向量的索引? [英] How to access to the index of a 2D vector by at() function?

查看:133
本文介绍了如何通过at()函数访问2D向量的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在成员函数中初始化一个2D向量,其中输入参数计划被输入到函数参数内的确定的索引中。
此外,我不会使用[]操作符成员访问由于安全的东西。
如何使用at()函数访问2D向量的索引,如下?

I am going to initialize a 2D vector in a member function, where the input arguments are planned to be fed into determined indexes within the function argument. Furthermore, I'm not gonna use [] operator to member access due to safety stuffs. How can I use at() function in order to access to the index of a 2D vector, as below?

vector<vector<double>> weight;

void Connection::setWeight(const double& value, const double& i, const double& j)
{
    // The other scheme except: weight[i][j] = value;
}


推荐答案

$ c>在两次。还要注意,如果 i j 是索引,它们实际上不应该是 double

You can use at twice. Also note, if i and j are indexes, they really shouldn't be double.

void Connection::setWeight(const double& value, const size_t& i, const size_t& j)
{
    weight.at(i).at(j) = value;
}

这篇关于如何通过at()函数访问2D向量的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆