小编Sou*_*rav的帖子

在我的朋友网络中找到最受欢迎的喜欢

我正在寻找一种在朋友网络中找到最受欢迎的喜欢的方法."在我的朋友网络中最受欢迎"被定义为"拥有我朋友最多的喜欢".

假设每个朋友都有一个唯一的ID,并且有许多喜欢的页面.因此,鉴于这样的朋友一个数组,我想找到它是由众多的朋友喜欢的喜欢,并且谁喜欢这个东西的朋友们.从本质上讲,我想展示"你的朋友X,Y&Z喜欢这个".

我的第一个解决方案是使用Map(用于存储反向映射:like-> set)和Priority Queue(用于查找前N个).这是我的算法(使用C++ STL):

map< like, set<friend> > like2friendsMap;
for each friend {
  for each like {
    like2friendsMap[like].insert(friend); //populate the map
  }
}

priority_queue< pair<like, int> > pq;
for each like in like2friendsMap {
  int count = like2friendsMap[like].size(); //no. of friends who like this or "popularity"
  pq.push(like, count); //count is the priority
}

map< like, set<friend> > result
for i in 1 to N { //N is how many popular items I want
   result = pq.top(); …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stl popularity top-n

5
推荐指数
1
解决办法
224
查看次数

为什么向 C 函数传递额外参数不会导致编译时错误?

我能够使用 gcc 4.4.7 编译并运行以下代码。

文件:MC

#include <stdio.h>

int main()
{
    printf("%d\n", f(1, 2, 3));
}
Run Code Online (Sandbox Code Playgroud)

文件:FC

int f(int a, int b)
{
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ gcc m.c f.c && ./a.out
$ 3
Run Code Online (Sandbox Code Playgroud)

当函数f()在同一文件中定义时,编译器会按预期抛出错误。我的猜测是编译器无法检测编译单元之间函数的错误使用。但链接器不应该能够检测到它吗?标准是否指定了预期的行为?

请注意,这与声明不带任何参数的函数不同,后者甚至可以在单个文件内运行。(为什么 gcc 允许将参数传递给定义为不带参数的函数?)。

我正在使用 gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) 和 GNU ld 版本 2.20.51.0.2-5.42.el6 20100205。

c linker gcc compiler-errors

4
推荐指数
1
解决办法
3489
查看次数

标签 统计

algorithm ×1

c ×1

c++ ×1

compiler-errors ×1

gcc ×1

linker ×1

popularity ×1

stl ×1

top-n ×1