随笔分类 - C++学习
摘要:为了确保任何区域的空间不被多于1个物体占用,我们需要基于物体间的空间信息来做碰撞检测。碰撞检测中重要的事情是有大量的测试,因此需要理由GPU资源。例如:如果我们有n个物体,一个物体将会碰撞n-1个物体(因为自己不会撞自己嘛),第二个物体撞剩下的n-2个。因此可能的碰撞是(n-1) * (n-2) *...
阅读全文
摘要:按照书上写的,不知道为什么有问题://已解决,参考最新的blog,哈哈#include #include #include #include struct foo{ int f_count; pthread_mutex_t f_lock;};struct foo* AllocFoo(){ foo* lpFoo = (foo*)malloc(sizeof(struct foo)); if(NULL != lpFoo) { lpFoo->f_count = 1; if(0 != pthread_mutex_init(&lpFoo->f_lo...
阅读全文
摘要:转自:http://www.cmnsoft.com/wordpress/?p=248感谢原作者。我在此整理一下:完成端口(IOCP)是WINDOWS平台上特有的一种技术。要使用IOCP技术,就要用到微软的WSA(windows socket api)。进行网络编程的套接口(socket)有UNIX套接口、伯克利套接口、WSA。其中使用最多的是伯克利套接口,因为他可在UNIX、WINDOWS、OS/2等计算机上使用。WSA套接口比伯克利套接口多了WSA三个字母。伯克利套接口:socket()、recv()、send()等。WSA套接口:WSASocket()、WSARecv()、WSASend(
阅读全文
摘要:这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威:Creates a thread to execute within the virtual address space of the calling process.在调用进程的虚拟地址空间里创建一个线程用CreateThread;To create a thread that runs in the virtual address space of another process, use theCreateRemoteThreadfunction.如果在另一进程的虚拟地址空间创建线程用CreateRemoteThr
阅读全文
摘要:之前写的服务器端 表示都无法收到client发的数据,找不到原因,原来是有个socket接收数据缓冲木有设置,现在设置后就可以正常收到数据啦!server端:#include #include #include #pragma comment(lib, "ws2_32.lib")#define MYPORT 1234 // the port users will be connecting to#define BACKLOG 5 // how many pending connections queue will hold#define BUF_SIZE 200int fd
阅读全文
摘要:算法导论第10章的东西,感觉用双链表真心简单,就是有点浪费空间,但是时间复杂度O(1):#include struct LinkNode{ LinkNode(): m_Value(-1) , m_pPreNode(NULL) , m_pNextNode(NULL) { } int m_Value; LinkNode* m_pPreNode; LinkNode* m_pNextNode;};class Queue{public: void Init() { m_pHeadNode = new LinkNod...
阅读全文
摘要:在网上找了个例子,其实libevent本身带了很多测试用例,不过这是第一次编译成功,尼玛 各种高性能IO库都是在linux下的,win32果断被bs了,还好有libevent对win32支持的比较好。lib很顺利的编译好了。下面就是很简单的使用下socket:#include #include #include #include #include #include #include #pragma comment(lib,"ws2_32.lib")#include #include #define LISTEN_PORT 9999#define LISTEN_BACKLOG
阅读全文
摘要:之前看到 偶尔用placement new 的用法,当分配内存频繁,而且对效率要求很高的情况下,可以先申请一块大内存,然后在此内存上构建对象,关键是可以自动调用其构造函数,否则要悲剧。但是之后要自己显式调用其析构函数,因为编译器也认为这是你new出来的,不会帮你调用,当然类在析构的时候做了两件事,调用析构函数,然后把内存还给os,对于placement new同样也需要这么做,但是要自己做: 1 #include 2 3 #include 4 #include 5 #include 6 7 using namespace std; 8 9 class A10 {11 public...
阅读全文
摘要:返回对象时,不能返回引用;其实在前几个item里就反复讲到operator的时候;Widget& operator=(int rhs){...return *this;}这里就返回了引用;但这个item里,作者又举出了反例;const Widgetoperator*(const Widget&,constWidget&){return Widget(....);}因为这样返回local 对象是很危险的,毕竟引用或指针所指的对象都消失了,还能操作它吗?很显然是很危险的。大师说的有道理,嗯,今天到这。
阅读全文
摘要:看来如果类比较复杂,自己写copying fun还是比较麻烦的事情:#include <iostream>#include<string>using namespace std;class Widget{public: Widget(int aData):m_Data(aData) { } virtual void Swap(Widget &aRhs) { int lTemp = aRhs.m_Data; aRhs.SetData(m_Data); m_Data = lTemp; } Widget& operator=(const Widget &
阅读全文
摘要:这个item主要分析了防止对象自我赋值,利用copy-and-swap策略来解决。写了小代码,其实在实际中大部分会简单写:#include <iostream>#include<string>using namespace std;class Widget{public: Widget(int aData):m_Data(aData) { } virtual void Swap(Widget &aRhs) { int lTemp = aRhs.m_Data; aRhs.SetData(m_Data); m_Data = lTemp; } Widget&
阅读全文
摘要:原来一句话就可以搞定了:int main(int argc, char *argv[]){ QApplication a(argc, argv); ** w; w.setWindowFlags(w.windowFlags()& ~Qt::WindowMaximizeButtonHint& ~Qt::WindowMinimizeButtonHint); w.show(); return a.exec();}
阅读全文
摘要:int GetInputStyle(){ if ("0x" == m_Str.substr(0,2)) return HexInput; boost::regex lRe("[2-9]"); boost::match_results<std::string::const_iterator> what; bool lSearchResult = boost::regex_search(m_Str.begin(), m_Str.end(), lRe); if(false == lSearchResult) { lRe = "[0-1]&
阅读全文
摘要:根据 博客园博主+石头+的原始代码,我修改为利用std::queue管理的消息队列:#include <iostream>#include <Windows.h>#include<queue>using namespace std;const int MAXMESSAGE = 1024;class CXMessageList{public: CXMessageList(); ~CXMessageList(){};public: MSG* GetTopMessage(); MSG* WaitForMessage(); int GetMessageCount()
阅读全文
摘要:摘自:http://www.cnblogs.com/jiayongzhi/archive/2011/05/09/2041655.html#include <iostream>#include <Windows.h>using namespace std;const int MAXMESSAGE = 1024;class CXMessageList{public: CXMessageList(void); ~CXMessageList(void){};public: MSG* GetTopMessage(); MSG* WaitForMessage(); int GetM
阅读全文
摘要:造了线程用select IO模型:#include <winsock.h>#include <stdio.h>#define PORT 5010#define MSGSIZE 1024#pragma comment(lib, "ws2_32.lib")int g_iTotalConn = 0;SOCKET g_CliSocketArr[FD_SETSIZE];DWORD WINAPI WorkerThread(LPVOID lpParameter);int main(){ WSADATA wsaData; SOCKET sListen, sClien
阅读全文
摘要:server:#include"WinSock2.h"#include "time.h"#include "stdio.h"#include <string>#include <iostream>#pragma comment(lib,"WS2_32")using namespace std;const int MaxLine = 1024;char lBuf[MaxLine];short gPort = 9877;struct Args{ long arg1; long arg2;};st
阅读全文
摘要:server:#include"WinSock2.h"#include "time.h"#include "stdio.h"#include <string>#include <iostream>#pragma comment(lib,"WS2_32")using namespace std;const int MaxLine = 1024;char lBuf[MaxLine];short gPort = 9877;int main(){ int lRet = 0; WSADATA lWsa
阅读全文
摘要:server:#include"WinSock2.h"#include "time.h"#include "stdio.h"#include <string>#include <iostream>#pragma comment(lib,"WS2_32")using namespace std;const int MaxLine = 1024;char lBuf[MaxLine];short gPort = 9877;int main(){ int lRet = 0; WSADATA lWsa
阅读全文
摘要:看到网上有一个示例,我查了先关函数,做了一些注释,自己学习下:#include <iostream>#include <string>#include <WinSock2.h>using namespace std;#pragma comment(lib, "WS2_32")int main() { WSAData wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); START
阅读全文