【问题标题】:Sorting (n x 2) array based on the first column using STL algorithm's sort ( ) function使用 STL 算法的 sort ( ) 函数根据第一列对 (n x 2) 数组进行排序
【发布时间】:2013-10-28 23:20:48
【问题描述】:

我想使用 C++ 算法库中的 sort() 函数根据其第一列对 (nx2) 数组进行排序。关于 sort() 函数中的自定义函数,我不知道如何使用 第三个参数。 下面的代码没有编译。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,k,i,j,missile[100000][2];
    scanf("%d",&n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d %d",&missile[i][0], &missile[i][1]);
    }

    bool sortFunc( int *missile[i][0], int *missile[j][0])
    {
        return missile[i][0]<missile[j][0];
    }

    sort(missile,missile+n,sortFunc);

    for (int i = 0; i < n; ++i)
    {
        printf("%d %d\n",missile[i][0],missile[i][1]);
    }


    return 0;
}

根据我的理解,我将代码更改为

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int n;
  scanf("%d",&n);
  vector< pair<int,int> > missile(n);
  for(auto p:missile)
     scanf("%d %d",&(p.first),&(p.second));

  sort(begin(missile),end(missile),less) //Using the default comparator for pair

  for(auto p:missile)
    printf("%d %d\n",p.first,p.second);
  return 0;
}

还是有很多编译错误

    hackerrank_missile.cpp: In function ‘int main()’:
    hackerrank_missile.cpp:12: error: a function-definition is not allowed here before ‘:’ token
    hackerrank_missile.cpp:15: error: ‘begin’ was not declared in this scope
    hackerrank_missile.cpp:15: error: ‘end’ was not declared in this scope
    hackerrank_missile.cpp:15: error: missing template arguments before ‘)’ token
    hackerrank_missile.cpp:17: error: expected `;' before ‘for’
    hackerrank_missile.cpp:17: error: expected primary-expression before ‘for’
    hackerrank_missile.cpp:17: error: expected `)' before ‘for’
    hackerrank_missile.cpp:17: error: a function-definition is not allowed here before ‘:’ token
    hackerrank_missile.cpp:19: error: expected primary-expression before ‘return’
    hackerrank_missile.cpp:19: error: expected `;' before ‘return’
    hackerrank_missile.cpp:19: error: expected primary-expression before ‘return’
    hackerrank_missile.cpp:19: error: expected `)' before ‘return’

【问题讨论】:

  • 你需要阅读更多关于std::sort的内容,尤其是比较函数及其参数。
  • 另外,你不能使用嵌套函数。为什么要使用 C 函数(如 scanfprintf)而不是标准 C++ I/O 函数?
  • 我还建议您使用 std::vectorstd::pair 来代替您的收藏。
  • 你也可以使用一个内部有 2 个变量的结构。然后重载'
  • 在 C++ 中,标准输入/输出 std::cinstd::cout

标签: c++ sorting stl multidimensional-array stl-algorithm


【解决方案1】:

此代码至少通过以下方式编译(使用 c++11,C 样式 IO 除外) clang++ test.cc -std=c++11 -stdlib=libc++ -Weverything -Wno-c++98-compat。 如果它符合你的要求,我不知道。

#include <cstdio>
#include <vector>
#include <algorithm>

int main() {
  std::size_t n;
  std::scanf("%ld",&n);
  std::vector<std::pair<int,int>> missile(n);
  for(auto&p:missile)
    std::scanf("%d %d",&(p.first),&(p.second));
  std::sort(std::begin(missile),std::end(missile),[](std::pair<int,int> const&i,
                                                     std::pair<int,int> const&j)
      { return i.first < j.first; });
  for(auto p:missile)
    std::printf("%d %d\n",p.first,p.second);
  return 0;
}

【讨论】:

  • 它没有在我的系统上编译。为什么你一次用 p 而另一次用 &p 和 'auto'
  • @Shivendra 您是否将-std=c++11 指定为编译器选项? auto&amp;p 确保p 的状态可以更改(读入),而auto p 不允许这样做(它将读入临时但不会读入vector&lt;&gt;)。
猜你喜欢
  • 2012-11-30
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多