相关疑难解决方法(0)

数组与链表

为什么有人想在阵列上使用链表?

毫无疑问,对链接列表进行编码比使用数组要多一些工作,人们可能想知道什么是合理的额外工作.

我认为在链表中插入新元素是微不足道的,但它是数组中的一项重要工作.使用链表存储一组数据与将其存储在数组中是否还有其他优点?

这个问题不是一个重复这个问题,因为其他的问题是关于一个特定的Java类专门询问,而这个问题的关注与一般的数据结构.

language-agnostic arrays linked-list data-structures

192
推荐指数
19
解决办法
22万
查看次数

从容器中获取前5个算法?

我有一个类(对象),User.该用户有2个私有属性,"名称"和"受欢迎程度".我将对象存储到向量(容器)中.

从容器中,我需要找到前5位最受欢迎的用户,我该怎么做?(我有一个丑陋的代码,我会在这里发布,如果你有更好的方法,请告诉我.如果你认为矢量不是一个好的选择,请随意使用其他容器,但请仅使用:map或multimap,列表,向量或数组,因为我只知道如何使用它们.)我目前的代码是:

int top5 = 0, top4 = 0, top3 = 0, top2 = 0, top1 = 0;
vector<User>::iterator it;

for (it = user.begin(); it != user.end(); ++it) 
{
    if( it->getPopularity() > top5){
        if(it->getPopularity() > top4){
            if(it->getPopularity() > top3){
                if(it->getPopularity() > top2){
                    if(it->getPopularity() > top1){
                        top1 = it->getPopularity();
                        continue;
                    } else {
                        top2 = it->getPopularity();
                        continue;
                    }
                } else {
                    top3 = it->getPopularity();
                    continue;
                }
            }
        } else {
            top4 = it->getPopularity();
            continue;
        }
    } else {
        top5 = it->getPopularity(); …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm

2
推荐指数
1
解决办法
609
查看次数