boost的使用(一)

最近项目中使用了boost的库,于是想把它学得更好,这里写点基础的用法,方便大家使用,虽然简单但是很实用。当然可以分析源码的高手就不用看这些东西了。使用的库为boost1.55版本。今天先从bind讲起吧,这个东西可以将函数的参数变为比原来参数个数少的函数,也可以找到类成员函数的指针。

好了以上坑爹的讲解完毕,发出代码来!

#include <boost/bind.hpp> 
#include <boost/function.hpp> 
#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <cstdlib> 
#include <cstring> 

void add(int i, int j)
{
	std::cout << i + j << std::endl;
}

bool compare(int i, int j)
{
	return i > j;
}


int main()
{
	std::vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);

	std::for_each(v.begin(), v.end(), boost::bind(add, 10,_1));
	std::sort(v.begin(), v.end(), boost::bind(compare, _1, _2));
	std::for_each(v.begin(), v.end(), boost::bind(add, 10, _1));


	boost::function<int(const char*)> f = std::atoi;
	std::cout << f("1609") << std::endl;
	f = std::strlen;
	std::cout << f("1609") << std::endl;

	system("pause");
}
这里给出了bind和function的用法,虽然简单,但是希望大家可以尝试着去使用改代码进行开发。简化自己的程序从一点一滴做起,提高慢慢开始。好了,不熟悉stl库的童鞋麻烦先去看stl用法,这里不予讲解,来讲解下 这一段代码,第一个传入我们自己默认的参数10,第二个参数传入从外边传入的参数。_1表示从外部传入的第一个参数。

最后啰嗦下 这表示一个模板函数,第一个int 代表函数是一个int型的返回值,括号中表示传入参数的类型是const char*!说明结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Boost是一个流行的、开源的C++集合,其中包含了许多实用的工具和组件,可以帮助我们更方便地开发高性能的网络应用程序。下面是一个使用Boost.Asio实现的简单的服务器框架: ```c++ #include <iostream> #include <boost/asio.hpp> using namespace boost::asio; using namespace boost::asio::ip; class Server { public: Server(int port) : acceptor_(io_service_, tcp::endpoint(tcp::v4(), port)) { start_accept(); io_service_.run(); } private: void start_accept() { TcpConnection::pointer new_connection = TcpConnection::create(io_service_); acceptor_.async_accept(new_connection->socket(), boost::bind(&Server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } void handle_accept(TcpConnection::pointer new_connection, const boost::system::error_code& error) { if (!error) { new_connection->start(); } start_accept(); } io_service io_service_; tcp::acceptor acceptor_; }; class TcpConnection : public boost::enable_shared_from_this<TcpConnection> { public: typedef boost::shared_ptr<TcpConnection> pointer; static pointer create(io_service& io_service) { return pointer(new TcpConnection(io_service)); } tcp::socket& socket() { return socket_; } void start() { boost::asio::async_read(socket_, boost::asio::buffer(buffer_), boost::bind(&TcpConnection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } private: TcpConnection(io_service& io_service) : socket_(io_service) { } void handle_read(const boost::system::error_code& error, size_t bytes_transferred) { if (!error) { std::cout << "Received message from client: " << std::string(buffer_.data(), bytes_transferred) << std::endl; } } tcp::socket socket_; boost::array<char, 1024> buffer_; }; int main() { try { Server server(8080); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << std::endl; } return 0; } ``` 在这个服务器框架中,我们使用Boost.Asio来处理网络通信,其中io_service是一个关键的组件,它负责管理异步I/O操作的事件循环。在Server类中,我们首先创建一个tcp::acceptor对象,然后调用其async_accept函数来监听客户端的连接请求。当有新的连接请求到来时,我们创建一个TcpConnection对象,并调用其start函数来开始接收客户端发送的消息。TcpConnection类继承自boost::enable_shared_from_this,这样我们可以在异步回调函数中安全地使用shared_from_this来获取当前对象的智能指针,避免了可能的内存泄漏问题。 当客户端发送消息时,我们在TcpConnection类中定义了handle_read函数来处理接收到的数据。在这个简单的实现中,我们只是简单地将数据打印到控制台上,实际上我们可以在这里进行更复杂的业务逻辑处理。 以上是一个简单的使用Boost实现的服务器框架,您可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值