首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

为什么我的"choose k from n“算法适用于std::vector而不适用于std::map?

"choose k from n"算法适用于std::vector而不适用于std::map的原因是因为std::vector是一个有序的线性容器,而std::map是一个基于红黑树实现的关联容器。

"choose k from n"算法是从n个元素中选择k个元素的组合算法。在std::vector中,元素是按照插入的顺序存储的,可以通过索引直接访问元素,因此可以通过下标操作来实现组合算法。例如,可以使用循环和递归的方式来生成所有可能的组合。

然而,在std::map中,元素是按照键值对的方式存储的,而不是按照插入的顺序。std::map使用红黑树来实现,这意味着元素是按照键的顺序进行排序的。由于std::map是基于二叉搜索树的数据结构,它不支持通过下标来访问元素。因此,在std::map中实现"choose k from n"算法会比较困难。

如果需要在std::map中实现"choose k from n"算法,可以考虑使用迭代器来遍历map中的元素,并使用递归的方式生成组合。但是这种方法效率较低,因为在红黑树中查找元素的时间复杂度是O(log n),而不是O(1)。因此,对于大规模的数据集,使用std::map来实现"choose k from n"算法可能会导致性能问题。

综上所述,"choose k from n"算法适用于std::vector而不适用于std::map的原因是std::vector是有序的线性容器,可以通过下标操作来访问元素,而std::map是基于红黑树实现的关联容器,不支持通过下标来访问元素,并且查找元素的时间复杂度较高。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

学了C++不会STL,简直少了左膀右臂

容器(Container): 是一种数据结构,如list,vector,和deques ,以模板类的方法提供。为了访问容器中的数据,可以使用由容器类输出的迭代器; 迭代器(Iterator): 提供了访问容器中对象的方法。例如,可以使用一对迭代器指定list或vector中的一定范围的对象。迭代器就如同一个指针。事实上,C++的指针也是一种迭代器。但是,迭代器也可以是那些定了operator*()以及其他类似于指针的操作符地方法的类对象; 算法(Algorithm): 是用来操作容器中的数据的模板函数。例如,STL用sort()来对一个vector中的数据进行排序,用find()来搜索一个list中的对象,函数本身与他们操作的数据的结构和类型无关,因此他们可以在从简单数组到高度复杂容器的任何数据结构上使用; 仿函数(Functor) 适配器(Adaptor) 分配器(allocator) 仿函数、适配器、与分配器用的比较少,甚至没用过!在这里不做说明,有兴趣可以自己学习一下,那个东西C++软件工程可能用的比较多。

02
  • 九度OJ——1154Jungle Roads

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems. 输入: The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to othe

    01

    String Modification (CodeCraft-20 (Div. 2))

    Vasya has a string s of length n. He decides to make the following modification to the string: Pick an integer k, (1≤k≤n). For i from 1 to n−k+1, reverse the substring s[i:i+k−1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length 2) weqr (after reversing the second substring of length 2) werq (after reversing the last substring of length 2) Hence, the resulting string after modifying s with k=2 is werq. Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a≠b; in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

    02

    自动驾驶定位算法(十三)-粒子滤波(Particle Filter)

    自动驾驶对定位的精度的要求在厘米级的,如何实现厘米级的高精度定位呢?一种众所周知的定位方法是利用全球定位系统(GPS),利用多颗卫星的测量结果,通过三角测量(Triangulation)机制确定目标的位置,GPS定位的原理见自动驾驶硬件系统(十一)-Global Navigation Satellite Systems (GNSS),但是GPS并不总是提供高精度定位数据,在GPS信号强的情况下,定位精度在1~3m范围内,在GPS信号弱的情况下,定位精度下降到10~50m范围内。虽然依赖于RTK,可以将卫星定位的精度提高到厘米级,但是在GPS信号弱的场景下,定位精度仍然不能满足应用需求。所以仅仅使用GPS不能实现高可靠的高精度定位的。

    01
    领券