c++ - 如何在 Windows Vista 上编译为 Linux/Unix 编写的 C++ 源代码(给出的代码)

标签 c++ unix windows-vista

我有一个其他作者在 linux/unix 环境下编写的 c++ 源代码。 当我在 windows vista 环境中编译它时它给我错误。我正在使用 Bloodshed Dev C++ v 4.9。请帮忙。

#include <iostream.h>
#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <strstream>
#include <unistd.h>
#include <stdlib.h>


using namespace std;

template <class T> class PrefixSpan {
private:
  vector < vector <T> >             transaction;
  vector < pair <T, unsigned int> > pattern;
  unsigned int minsup;
  unsigned int minpat;
  unsigned int maxpat;
  bool all;
  bool where;
  string delimiter;      
  bool verbose;
  ostream *os;

  void report (vector <pair <unsigned int, int> > &projected) 
  {
    if (minpat > pattern.size()) return;

    // print where & pattern
    if (where) { 
      *os << "<pattern>" << endl;

      // what:
      if (all) {
 *os << "<freq>" << pattern[pattern.size()-1].second << "</freq>" << endl;
 *os << "<what>";
 for (unsigned int i = 0; i < pattern.size(); i++) 
   *os << (i ? " " : "") << pattern[i].first;
      } else {
 *os << "<what>";
  for (unsigned int i = 0; i < pattern.size(); i++)
    *os << (i ? " " : "") << pattern[i].first 
        << delimiter << pattern[i].second;
      }

      *os << "</what>" << endl;

      // where
      *os << "<where>";
      for (unsigned int i = 0; i < projected.size(); i++) 
 *os << (i ? " " : "") << projected[i].first;
      *os << "</where>" << endl;

      *os << "</pattern>" << endl;

    } else {

      // print found pattern only
      if (all) {
  *os << pattern[pattern.size()-1].second;
  for (unsigned int i = 0; i < pattern.size(); i++)
    *os << " " << pattern[i].first;
      } else {
  for (unsigned int i = 0; i < pattern.size(); i++)
    *os << (i ? " " : "") << pattern[i].first
        << delimiter << pattern[i].second;
      }

      *os << endl;
    }
  }

  void project (vector <pair <unsigned int, int> > &projected)
  {
    if (all) report(projected);

    map <T, vector <pair <unsigned int, int> > > counter;

    for (unsigned int i = 0; i < projected.size(); i++) {
      int pos = projected[i].second;
      unsigned int id  = projected[i].first;
      unsigned int size = transaction[id].size();
      map <T, int> tmp;
      for (unsigned int j = pos + 1; j < size; j++) {
 T item = transaction[id][j];
 if (tmp.find (item) == tmp.end()) tmp[item] = j ;
      }

      for (map <T, int>::iterator k = tmp.begin(); k != tmp.end(); ++k) 
 counter[k->first].push_back (make_pair <unsigned int, int> (id, k->second));
    }

    for (map <T, vector <pair <unsigned int, int> > >::iterator l = counter.begin (); 
  l != counter.end (); ) {
      if (l->second.size() < minsup) {
 map <T, vector <pair <unsigned int, int> > >::iterator tmp = l;
 tmp = l;
 ++tmp;
 counter.erase (l);
 l = tmp;
      } else {
 ++l;
      }
    }

    if (! all && counter.size () == 0) {
      report (projected);
      return;
    }

    for (map <T, vector <pair <unsigned int, int> > >::iterator l = counter.begin (); 
  l != counter.end(); ++l) {
      if (pattern.size () < maxpat) {
  pattern.push_back (make_pair <T, unsigned int> (l->first, l->second.size()));
  project (l->second);
  pattern.erase (pattern.end());
      }
    }
  }

public:
  PrefixSpan (unsigned int _minsup = 1,
       unsigned int _minpat = 1,        
       unsigned int _maxpat = 0xffffffff,
       bool _all = false,
       bool _where = false,
       string _delimiter = "/",
       bool _verbose = false):
    minsup(_minsup), minpat (_minpat), maxpat (_maxpat), all(_all), 
    where(_where), delimiter (_delimiter),  verbose (_verbose) {};

  ~PrefixSpan () {};

  istream& read (istream &is) 
  {
    string line;
    vector <T> tmp;
    T item;
    while (getline (is, line)) {
       tmp.clear ();
       istrstream istrs ((char *)line.c_str());
       while (istrs >> item) tmp.push_back (item);
       transaction.push_back (tmp);
    }

    return is;
  }

  ostream& run (ostream &_os)
  {
    os = &_os;
    if (verbose) *os << transaction.size() << endl;
    vector <pair <unsigned int, int> > root;
    for (unsigned int i = 0; i < transaction.size(); i++) 
      root.push_back (make_pair (i, -1));
     project (root); 
    return *os;
  }

  void clear ()
  {
    transaction.clear ();
    pattern.clear ();
  }
};

int main (int argc, char **argv)
{
  extern char *optarg;
  unsigned int minsup = 1;
  unsigned int minpat = 1;
  unsigned int maxpat = 0xffffffff;
  bool all = false;
  bool where = false;
  string delimiter = "/";
  bool verbose = false;
  string type = "string"; 

  int opt;
  while ((opt = getopt(argc, argv, "awvt:M:m:L:d:")) != -1) {
    switch(opt) {
    case 'a':
      all = true;
      break;
    case 'w':
      where = true;
      break;
    case 'v':
      verbose = true;
      break;
    case 'm':
      minsup = atoi (optarg);
      break;
    case 'M':
      minpat = atoi (optarg);
      break;
    case 'L':
      maxpat = atoi (optarg);
      break;
    case 't':
      type = string (optarg); 
      break;
    case 'd':
      delimiter = string (optarg);
      break;
    default:
      cout << "Usage: " << argv[0] 
    << " [-m minsup] [-M minpat] [-L maxpat] [-a] [-w] [-v] [-t type] [-d delimiter] < data .." << endl;
      return -1;
    }
  }

  if (type == "int") { 
     PrefixSpan<unsigned int> prefixspan (minsup, minpat, maxpat, all, where, delimiter, verbose);
     prefixspan.read (cin);
     prefixspan.run  (cout);
  }else if (type == "short") {
     PrefixSpan<unsigned short> prefixspan (minsup, minpat, maxpat, all, where, delimiter, verbose);
     prefixspan.read (cin);
     prefixspan.run  (cout);
  } else if (type == "char") {
     PrefixSpan<unsigned char> prefixspan (minsup, minpat, maxpat, all, where, delimiter, verbose);
     prefixspan.read (cin);
     prefixspan.run  (cout);
  } else if (type == "string") {
     PrefixSpan<string> prefixspan (minsup, minpat, maxpat, all, where, delimiter, verbose);
     prefixspan.read (cin);
     prefixspan.run  (cout);
  } else { 
     cerr << "Unknown Item Type: " << type << " : choose from [string|int|short|char]" << endl;
     return -1;
  }

  return 0;
}

最佳答案

您可以通过删除 包含并消除对 getopt() 的使用,使其在 Windows 上运行功能...您将不得不手动解析命令行或使用boost::program_options .

header 和 getopt() 函数在符合 UNIX 的操作系统上均可用,但在 Windows 上不可用(Windows 显然不兼容)。如果您希望能够在不更改任何源代码的情况下进行编译,您也可以尝试下载并安装 Cygwin ,它试图在 Windows 内部提供一个与 UNIX 兼容的环境,并取得了一点成功(尽管它无论如何都不完美)。或者,将来您可以使用跨平台库,例如 BoostQt .

手动解析
根据您的使用信息,您可以将 while 循环替换为以下代码:

int idx=1;
while ( idx < argc ){
    std::string arg(argv[idx]);
    if (arg == "-m"){
        //minsup 
        idx++;
        if (idx>argc){ 
           std::cerr<<"Option \""<<arg<<"\" requires parameter."<<std::endl;
           usage(argv[0]); // move usage message into a function
           std::exit(1);
        }
        minsup=atoi(argv[idx++]);
    }else if (arg == "-M"){
        //minpat
        idx++;
        if (idx>argc){ 
           std::cerr<<"Option \""<<arg<<"\" requires parameter."<<std::endl;
           usage(argv[0]); // move usage message into a function
           std::exit(1);
        }
        minpat=atoi(argv[idx++]);
    }else if (arg == "-L"){
        //maxpat
        idx++;
        if (idx>argc){ 
           std::cerr<<"Option \""<<arg<<"\" requires parameter."<<std::endl;
           usage(argv[0]); // move usage message into a function
           std::exit(1);
        }
        maxpat=atoi(argv[idx++]);
    }else if (arg == "-a"){
        all=true;
        idx++;
    }else if (arg == "-w"){
        where=true;
        idx++;
    }else if (arg == "-v"){
        verbose=true;
        idx++;
    }else if (arg == "-t"){
        //type
        idx++;
        if (idx>argc){ 
           std::cerr<<"Option \""<<arg<<"\" requires parameter."<<std::endl;
           usage(argv[0]); // move usage message into a function
           std::exit(1);
        }
        type=argv[idx++];
    }else if (arg == "-d"){
        idx++;
        if (idx>argc){ 
           std::cerr<<"Option \""<<arg<<"\" requires parameter."<<std::endl;
           usage(argv[0]); // move usage message into a function
           std::exit(1);
        }
        delimiter=argv[idx++];
    }else {
        usage();
        std::exit(1);
    }
}

然后在你的主函数之前的某处添加下面的代码:

void usage(const char* progname)
{
    std::cout << "Usage: " << progname << " [-m minsup] [-M minpat] [-L maxpat] [-a] [-w] [-v] [-t type] [-d delimiter]" < data .." << endl;
}

请注意,这与 getopt 的实际行为略有不同(getopt 允许您将 -awv 组合在一起,而此解析不会......但如果这不重要,那么这应该可以完成工作。

关于c++ - 如何在 Windows Vista 上编译为 Linux/Unix 编写的 C++ 源代码(给出的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2627388/

相关文章:

c++ - 为什么启用未定义的行为清理会干扰优化?

c - 使用 Makefile 将目标文件存储在两个不同的目录中? [C]

c++ - 如何检查文件是否在C++中可执行?

windows-vista - 在设备管理器中以编程方式禁用设备 (Vista x64)

c++ - C++中的 "type of expression"是什么?

c++ - RAII 的有用性无一异常(exception)

http - netsh http 添加 urlacl : add reservation for a group

c# - 寻找用于检测可移动驱动器(USB 闪存)的 C# 代码

c++ - 为类的实例提供指向结构的指针

c++ - 是否可以在编译时获取时间(当天)和日期?