如何在结构或类的向量中快速搜索具有特定值的对象? c ++ [英] How to make a fast search for an object with a particular value in a vector of structs or classes? c++

查看:167
本文介绍了如何在结构或类的向量中快速搜索具有特定值的对象? c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在向量中有成千上万个结构或类对象,如何以快速方式找到所需的结构或类对象?

例如:

制作游戏,我需要最快的碰撞检测方法。每个瓦片是一个结构,在矢量地图中有许多瓦片,有一个值:x和y。
所以基本上我做:

If I have thousands of struct or class objects in a vector, how to find those that are needed, in a fast way?
For example:
Making a game, and I need fastest way of collision detection. Each tile is a struct, there are many tiles in the vector map, with a values: x and y. So basically I do:

For(i=0;i<end of vector list;i++)
{
 //searching if x= 100 and y =200
}



<

So maybe there is a different way , like smart pointers or something to search for particular objects faster?

推荐答案

你应该 排序 您的矢量,然后使用标准库算法,如 binary_search lower_bound upper_bound

You should sort your vector and then use the standard library algorithms like binary_search, lower_bound, or upper_bound.

上面的例子会给你一个更好的顺从性,比通过整个向量的遍历给出的 o(n)使用标准库算法 查找

The above will give you a better compliexity than o(n) given by walk through of entire vector or by using standard library algorithm find.

这篇关于如何在结构或类的向量中快速搜索具有特定值的对象? c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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