可以安全地修改成对向量中的 std::pair::first 吗?
我目前正在研究 DNA 数据库类,并且目前将数据库中的每一行与匹配分数(基于编辑距离)和实际 DNA 序列本身相关联,在迭代循环中首先以这种方式修改是否安全?
typedef std::pair<int, DnaDatabaseRow> DnaPairT;
typedef std::vector<DnaPairT> DnaDatabaseT;
// ....
for(DnaDatabaseT::iterator it = database.begin();
it != database.end(); it++)
{
int score = it->second.query(query);
it->first = score;
}
我这样做的原因是为了以后可以按分数对它们进行排序。 我尝试过地图并收到有关首先修改的编译错误,但是是否有比这更好的方法来存储所有信息以供稍后排序?
I'm currently working on a DNA database class and I currently associate each row in the database with both a match score (based on edit distance) and the actual DNA sequence itself, is it safe to modify first this way within an iteration loop?
typedef std::pair<int, DnaDatabaseRow> DnaPairT;
typedef std::vector<DnaPairT> DnaDatabaseT;
// ....
for(DnaDatabaseT::iterator it = database.begin();
it != database.end(); it++)
{
int score = it->second.query(query);
it->first = score;
}
The reason I am doing this is so that I can sort them by score later. I have tried maps and received a compilation error about modifying first, but is there perhaps a better way than this to store all the information for sorting later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答你的第一个问题,是的。 修改对的成员是完全安全的,因为对中的实际数据不会影响向量本身。
编辑:我有一种感觉,您在使用地图时遇到错误,因为您尝试修改地图内部对的
first
值。 这是不允许的,因为该值是地图内部工作的一部分。正如dribeas所述:
编辑:要回答你的第二个问题,我认为你构建数据的方式没有任何问题,但我会让数据库保存指向
DnaPairT的指针
对象,而不是对象本身。 这将大大减少排序过程中复制的内存量。您可能需要研究更有效的方法的唯一原因是您的数据库太大以至于排序循环需要很长时间才能完成。 但如果是这种情况,我想您的
query
函数将占用大部分处理时间。To answer your first question, yes. It is perfectly safe to modify the members of your pair, since the actual data in the pair does not affect the vector itself.
edit: I have a feeling that you were getting an error when using a map because you tried to modify the
first
value of the map's internal pair. That would not be allowed because that value is part of the map's inner workings.As stated by dribeas:
edit: To answer your second question, I see nothing at all wrong with the way you are structuring the data, but I would have the database hold pointers to
DnaPairT
objects, instead of the objects themselves. This would dramatically reduce the amount of memory that gets copied around during the sort procedure.The only reason you might need to look into more efficient methods is if your database is so enormous that the sorting loop takes too long to complete. If that is the case, though, I would imagine that your
query
function would be the one taking up most of the processing time.您无法修改,因为 std::pair 的变量第一个被定义为 const
You can't modify since the variable first of std::pair is defined const