美文网首页
map中使用仿函数

map中使用仿函数

作者: 陶者001 | 来源:发表于2021-09-24 09:43 被阅读0次

class myPoint {

  int x;

  int y;

  bool operator<(const myPoint &in) const {

    return (in.x * in.x + in.y * in.y) < (x * x + y * y);

  }

};

class myCmp {

 public:

  bool operator()(const string &a, const string &b) { return a > b; }

};

struct myNode {

  map<string, myNode *, myCmp> myList;

};

vector<string> spli(string &path) {

  vector<string> res;

  path += '/';

  int pos = 0;

  while (pos = path.find('/')) {

    if (pos == string::npos) {

      break;

    }

    res.push_back(path.substr(0, pos));

    path = path.substr(pos + 1);

  }

  return res;

}

void insertTree(myNode *root, string path) {

  vector<string> names = spli(path);

  myNode *cur = root;

  for (string name : names) {

    auto &curList = cur->myList;

    if (curList.find(name) == curList.end()) {

      curList[name] = new myNode();

    }

    cur = curList[name];

  }

}

void printTree(myNode *root, int tab) {

  if (root == NULL) {

    return;

  }

  for (auto node : root->myList) {

    for (int i = tab; i > 0; i--) {

      cout << " ";

    }

    cout << node.first << endl;

    printTree(node.second, tab + 1);

  }

}

myNode *test_buildTree(vector<string> &paths) {

  myNode *root = new myNode();

  for (auto path : paths) {

    insertTree(root, path);

  }

  printTree(root, 0);

  return NULL;

}

void mytest() {

  vector<string> tmp({"c/d/e/f", "c/f", "c/e", "c/b/", "e/f/"});

  PrintTable(tmp);

  cout << "--------------" << endl;

  test_buildTree(tmp);

  getchar();

}

相关文章

网友评论

      本文标题:map中使用仿函数

      本文链接:https://www.haomeiwen.com/subject/tnakoctx.html