简体   繁体   English

如何在C ++中使用堆栈,队列或BST对文件中的记录进行排序?

[英]How to sort records in a file using a stack, queue, or BST in C++?

If you have a .dat file with records such as: 如果您的.dat文件包含以下记录:

805816899 Andrew
803975268 Bob
912684297 Jeff
123546789 Louis
751354687 Kevin

What would be the easiest data structure to use in order to sort the list by ID number and then write to screen? 为了按ID号对列表进行排序然后写入屏幕,最容易使用的数据结构是什么? I figure a BST would make the most sense and be the most efficient, but a stack would be easier and faster when dealing with a small file such as this. 我认为BST最为有意义,而且效率最高,但是处理诸如此类的小文件时,堆栈将变得更加轻松快捷。

Also, how would you implement it? 另外,您将如何实施它?

The simplest was to sort them using a BST would be to put them in an std::map<int, std::string> . 最简单的方法是使用BST对它们进行排序,将它们放在std::map<int, std::string> This is a self sorted data structure using a BST internally (although that isn't explicitly specified by the standard). 这是内部使用BST的自排序数据结构(尽管标准未明确指定)。 If you do not want look-up, you can use an std::set of a user defined type instead (see next paragraph). 如果您不想查找,则可以使用用户定义类型的std::set (请参见下一段)。

If you want to store them in a flat array-like structure, you could create a small struct to hold the information, store instances of it in an std::vector , and use a suitable comparison function in combination with std::sort to do the sorting. 如果要将它们存储在类似平面数组的结构中,则可以创建一个小结构来保存信息,将其实例存储在std::vector ,然后将合适的比较函数与std::sort结合使用以进行排序。

BST: BST:

#include <string>
#include <map>
std::map<int, std::string> m;
m[805816899] = "Andrew";
m[803975268] = "Bob";

and so on. 等等。

array-like solution: 类似数组的解决方案:

struct Foo 
{ 
  Foo(int ID, const std::string& name) : ID(ID), name(name) {}
  int ID; 
  std::string name;
};
// comparison for sorting
bool comp(const Foo& lhs, const Foo& rhs) { return lhs.ID < rhs.ID; }

#include <vector>
#include <algorithm>
....
std::vector<Foo> v;
v.push_back(Foo(805816899, "Andrew"));
v.push_back(Foo(803975268, "Bob"));
// add more entries
std::sort(v.begin(), v.end(), comp);

I think, a trie is the easiest way to implement the whole sorting and it's O(n) if the length of the id is constant. 我认为, 特里是实现整个排序的最简单方法,如果id的长度是常数,则为O(n)。

#include <iostream>
#include <cassert>
#include <string>

class node {
  bool final;
  union {
    node *next[10];
    char *name;
  };
  node *get_at(int index)
  {
    assert(!final);
    return next[index] ? next[index] : (next[index] = new node);
  }

public:
  node() : final(false) { std::fill(next, next+10, (node*)0); }
  ~node() {
    if (final)
      delete name;
    else
      for (int i=0; i<10; ++i)
        delete next[i];
  }

  void insert(const char *id, std::string const& s)
  {
    if (*id) {
      get_at(*id - '0')->insert(id+1, s);
    } else {
      final=true;
      name = new char[s.size()+1]();
      std::copy(s.begin(), s.end(), name);
    }
  }

  void print_all(std::ostream& os) const
  {
    if (final)
      os << name << '\n';
    else
      for (int i = 0; i < 10; ++i)
        if (next[i])
          next[i]->print_all(os);
  }
};

int main()
{
  node top;
  for (std::string id, name; std::cin >> id >> name;)
    top.insert(id.c_str(), name);
  top.print_all(std::cout);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM