配置Dlib环境: 链接
1.内存管理allocator
一个例子:把vector和String内存的分配和回收都用Dlib的内存管理机制处理了……
// 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 dlib::std_allocator object.
In this example we will create the necessary typedefs to give the
dlib::std_allocator object to the standard string and vector objects
in the STL. Thus we will create versions of std::string and std::vector
that perform all their memory allocations and deallocations via one of
the dlib memory manager objects.
*/
// include everything we need for this example
#include <vector>
#include <iostream>
#include <string>
#include <dlib/std_allocator.h>
#include <dlib/memory_manager.h>
#include <dlib/memory_manager_stateless.h>
using namespace std;
using namespace dlib;
int main()
{
// Make a typedef for an allocator that uses the thread safe memory_manager_stateless object with a
// global memory pool. This version of the memory_manager_stateless object keeps everything it allocates
// in a global memory pool and doesn't release any memory until the program terminates.
typedef std_allocator<char, memory_manager_stateless<char>::kernel_2_3a> alloc_char_with_global_memory_pool;
// Now make a typedef for a C++ standard string that uses our new allocator type
typedef std::basic_string<char, char_traits<char>, alloc_char_with_global_memory_pool > dstring;
// typedef another allocator for dstring objects
typedef std_allocator<dstring, memory_manager_stateless<char>::kernel_2_3a> alloc_dstring_with_global_memory_pool;
// Now make a typedef for a C++ standard vector that uses our new allocator type and also contains the new dstring
typedef std::vector<dstring, alloc_dstring_with_global_memory_pool > dvector;
// Now we can use the string and vector we have as we normally would. So for example, I can make a
// dvector and add 4 strings into it like so:
dvector v;
v.push_back("one");
v.push_back("two");
v.push_back("three");
v.push_back("four");
// And now we print out the contents of our vector
for (unsigned long i = 0; i < v.size(); ++i)
{
cout << v[i] << endl;
}
system("pause");
}
2.成员功能指针member_function_pointer
直观上看上去好像挺有用的,但又觉得短期不怎么用得上……
// 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 member_function_pointer object
from the dlib C++ Library.
*/
#include <iostream>
#include <dlib/member_function_pointer.h>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
class example_object
{
public:
void do_something(
)
{
cout << "hello world" << endl;
}
void print_this_number(
int num
)
{
cout << "number you gave me = " << num << endl;
}
};
// ----------------------------------------------------------------------------------------
int main()
{
// create a pointer that can point to member functions that take no arguments
member_function_pointer<> mfp1;
// create a pointer that can point to member functions that take a single int argument
member_function_pointer<int> mfp2;
example_object obj;
// now we set the mfp1 pointer to point to the member function do_something()
// on the obj object.
mfp1.set(obj, &example_object::do_something);
// now we set the mfp1 pointer to point to the member function print_this_number()
// on the obj object.
mfp2.set(obj, &example_object::print_this_number);
// Now we can call the function this pointer points to. This calls the function
// obj.do_something() via our member function pointer.
mfp1();
// Now we can call the function this pointer points to. This calls the function
// obj.print_this_number(5) via our member function pointer.
mfp2(5);
// The above example shows a very simple use of the member_function_pointer.
// A more interesting use of the member_function_pointer is in the implementation
// of callbacks or event handlers. For example, when you register an event
// handler for a dlib::button click it uses a member_function_pointer
// internally to save and later call your event handler.
system("pause");
}
// ----------------------------------------------------------------------------------------
3.Logger(用来记录信息)
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This is a simple example illustrating the use of the logger object from
the dlib C++ Library.
The output of this program looks like this:
0 INFO [0] example: This is an informational message.
0 DEBUG [0] example: The integer variable is set to 8
0 WARN [0] example: The variable is bigger than 4! Its value is 8
0 INFO [0] example: we are going to sleep for half a second.
503 INFO [0] example: we just woke up
503 INFO [0] example: program ending
The first column shows the number of milliseconds since program start at the time
the message was printed, then the logging level of the message, then the thread that
printed the message, then the logger's name and finally the message itself.
*/
#include <dlib/logger.h>
#include <dlib/misc_api.h>
using namespace dlib;
// Create a logger object somewhere. It is usually convenient to make it at the global scope
// which is what I am doing here. The following statement creates a logger that is named example.
logger dlog("example");
int main()
{
// Every logger has a logging level (given by dlog.level()). Each log message is tagged with a
// level and only levels equal to or higher than dlog.level() will be printed. By default all
// loggers start with level() == LERROR. In this case I'm going to set the lowest level LALL
// which means that dlog will print all logging messages it gets.
dlog.set_level(LALL);
// print our first message. It will go to cout because that is the default.
dlog << LINFO << "This is an informational message.";
// now print a debug message.
int variable = 8;
dlog << LDEBUG << "The integer variable is set to " << variable;
// the logger can be used pretty much like any ostream object. But you have to give a logging
// level first. But after that you can chain << operators like normal.
if (variable > 4)
dlog << LWARN << "The variable is bigger than 4! Its value is " << variable;
dlog << LINFO << "we are going to sleep for half a second.";
// sleep for half a second
dlib::sleep(500);
dlog << LINFO << "we just woke up";
dlog << LINFO << "program ending";
system("pause");
}
有点方,接着去做手势识别了…