接触到 std 的 unique 这个函数,看了一下实现,自己也写了一下。在读默认的实现的时候发现代码真是的比较坑爹的,缩进啊,花括号啊,之类的,都是不按规范来的。
自己照着写了一个如下
#include <iostream>
#include <vector>
template<class It>
It myUnique(It itBegin, It itEnd) {
…… 阅读全文
Tag Archives: C++
c++ 模版类调用不同函数
我们都知道,c++ 用模版,可以根据不同的对象类型,生成不同的实际函数,但是,如果我想根据不同的条件,在一个模版类里面,调用不同的函数呢,能不能这样写
#include <iostream>
using namespace std;
void PrintA(int a)
{
cout<<a<<endl;
}
void PrintB(int b)
{
cout<…… 阅读全文
Google perftools cpu profiler 对多进程的支持问题
折腾了好久,到目前还是没有让 gperftools cpu profiler 能在多进程环境下跑起来,问题如下:
main_cmd_argv.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <gperftool…… 阅读全文
strncpy 的用法
在官方的文档中,http://www.cplusplus.com/refer…,例程的 strncpy 的用法是:
/* strncpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to size…… 阅读全文
使用代码块来缩小变量作用域
今天突发奇想,想到这么一个写法
#include <stdio.h>
int main() {
char c = 'A';
{
int a = 1;
printf("%d\n", a);
printf("%c\n", c);
}
{
int a = 2;
printf("%d\n", a);
printf("%c\n&qu…… 阅读全文
Linux 多进程监听 socket
首先来看代码
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/wait.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd…… 阅读全文
Linux TCP UDP 混合
基本的思路是这样的:在服务器端,有两个机器,一个对外开启 TCP 监听,然后把监听到的请求内容送到后面的另外一台,或者多台轮询机器上,内网之间使用 UDP,然后等待业务逻辑机器处理完成,这个地方可以做成异步的,然后再返回到用户。
即是这样: client server_front server_back
下面来看代码,首先是 server_f…… 阅读全文
Linux UDP 收发实例
首先我们需要一个 struct.h
#ifndef STRUCT_H
#define STRUCT_H
typedef struct _UDP_MSG {
int add1;
int add2;
int sum;
char str1[128];
char str2[128];
char cat[256];
} UDP_MSG;
#endif /* STRUCT_H */
然后是服务端的
#include <sys/types.h>…… 阅读全文
共享内存的读写
首先我们需要一个 shm_com.h
#ifndef _SHM_COM_H_
#define _SHM_COM_H_
#define TEXT_SZ 2048
struct shared_use_st {
int written_by_you;
char some_text[TEXT_SZ];
};
#endif /* _SHM_COM_H */
然后是 shm1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <…… 阅读全文
[转]一份高质量的 Epoll 服务端代码
来自https://banu.com/blog/2/how-to…,由http://www.oschina.net/transla…提及
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <…… 阅读全文