Dlib库【5】——分配任务,遍历目录,队列queue的用法

来源:互联网 发布:ty66 永久域名 编辑:程序博客网 时间:2024/06/08 01:09

配置Dlib环境:  链接

1.最大价值分配任务max_cost_assignment

一个功能函数max_cost_assignment可以计算出一个分配任务的最大价值方案


解决的问题——一个矩阵,n*n,每一行取一个,每列也只能取一个,求最大和…


// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This simple example shows how to call dlib's optimal linear assignment problem solver.It is an implementation of the famous Hungarian algorithm and is quite fast, operating inO(N^3) time.*/#include <dlib/optimization/max_cost_assignment.h>#include <iostream>using namespace std;using namespace dlib;int main(){// Let's imagine you need to assign N people to N jobs.  Additionally, each person will make// your company a certain amount of money at each job, but each person has different skills// so they are better at some jobs and worse at others.  You would like to find the best way// to assign people to these jobs.  In particular, you would like to maximize the amount of// money the group makes as a whole.  This is an example of an assignment problem and is// what is solved by the max_cost_assignment() routine.// // So in this example, let's imagine we have 3 people and 3 jobs.  We represent the amount of// money each person will produce at each job with a cost matrix.  Each row corresponds to a// person and each column corresponds to a job.  So for example, below we are saying that// person 0 will make $1 at job 0, $2 at job 1, and $6 at job 2.  matrix<int> cost(3, 3);cost = 1, 2, 6,5, 3, 6,4, 5, 0;// To find out the best assignment of people to jobs we just need to call this function.std::vector<long> assignment = max_cost_assignment(cost);// This prints optimal assignments:  [2, 0, 1] which indicates that we should assign// the person from the first row of the cost matrix to job 2, the middle row person to// job 0, and the bottom row person to job 1.for (unsigned int i = 0; i < assignment.size(); i++)cout << assignment[i] << std::endl;// This prints optimal cost:  16.0// which is correct since our optimal assignment is 6+5+5.cout << "optimal cost: " << assignment_cost(cost, assignment) << endl;system("pause");}

2.遍历目录

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This is an example illustrating the use of the dir_nav component from the dlib C++ Library.It prints a listing of all directories and files in the userscurrent working directory or the directory specified on the command line.*/#include <iostream>#include <iomanip>#include <dlib/dir_nav.h>#include <vector>#include <algorithm>using namespace std;using namespace dlib;int main(int argc, char** argv){try{string loc;if (argc == 2)loc = argv[1];elseloc = ".";  // if no argument is given then use the current working dir.directory test(loc);cout << "directory: " << test.name() << endl;cout << "full path: " << test.full_name() << endl;cout << "is root:   " << ((test.is_root()) ? "yes" : "no") << endl;// get all directories and files in teststd::vector<directory> dirs = test.get_dirs();std::vector<file> files = test.get_files();// sort the files and directoriessort(files.begin(), files.end());sort(dirs.begin(), dirs.end());cout << "\n\n\n";// print all the subdirectoriesfor (unsigned long i = 0; i < dirs.size(); ++i)cout << "        <DIR>    " << dirs[i].name() << "\n";// print all the subfilesfor (unsigned long i = 0; i < files.size(); ++i)cout << setw(13) << files[i].size() << "    " << files[i].name() << "\n";cout << "\n\nnumber of dirs:  " << dirs.size() << endl;cout << "number of files: " << files.size() << endl;}catch (file::file_not_found& e){cout << "file not found or accessible: " << e.info << endl;}catch (directory::dir_not_found& e){cout << "dir not found or accessible: " << e.info << endl;}catch (directory::listing_error& e){cout << "listing error: " << e.info << endl;}system("pause");}

3.队列queue的用法

enqueue入队

move_next()移动到下一个

element为当前的元素

dequeue出队


// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt/*This is an example illustrating the use of the queue component (andto some degree the general idea behind most of the other containerclasses) from the dlib C++ Library.It loads a queue with 20 random numbers.  Then it uses the enumerableinterface to print them all to the screen.  Then it sorts the numbers andprints them to the screen.*/#include <dlib/queue.h>#include <iostream>#include <iomanip>#include <ctime>#include <cstdlib>// I'm picking the version of the queue that is kernel_2a extended by// the queue sorting extension.   This is just a normal queue but with the// added member function sort() which sorts the queue.typedef dlib::queue<int>::sort_1b_c queue_of_int;using namespace std;using namespace dlib;int main(){queue_of_int q;// initialize rand()srand(time(0));for (int i = 0; i < 20; ++i){int a = rand() & 0xFF;// note that adding a to the queue "consumes" the value of a because// all container classes move values around by swapping them rather// than copying them.   So a is swapped into the queue which results // in a having an initial value for its type (for int types that value// is just some undefined value. )q.enqueue(a);}cout << "The contents of the queue are:\n";while (q.move_next())cout << q.element() << " ";cout << "\n\nNow we sort the queue and its contents are:\n";q.sort();  // note that we don't have to call q.reset() to put the enumerator   // back at the start of the queue because calling sort() does   // that automatically for us.  (In general, modifying a container   // will reset the enumerator).while (q.move_next())cout << q.element() << " ";cout << "\n\nNow we remove the numbers from the queue:\n";while (q.size() > 0){int a;q.dequeue(a);cout << a << " ";}cout << endl;system("pause");}

感觉一篇文章里放仨已经够多了……那就先这样吧~





原创粉丝点击