最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

后台开发核心技术与应用实践——书籍阅读笔记

互联网 admin 0浏览 0评论

后台开发核心技术与应用实践——书籍阅读笔记

目录

  • 前言
  • 正文
    • 第三章——常见STL的使用
      • 1. 字符指针和字符数组的使用注意要点
      • 2. String类的书写
      • 3. C++字符串与C字符串的相互转换
      • 4. string 转int的方法
    • 第7章:网络IO模型
      • 1. select 函数
  • 总结
  • 参考

前言

这本书在挺多地方都看到推荐了,那就花点时间看一下,看一下人家总结的思路,在前人的基础上继续往前走。

正文

第三章——常见STL的使用

1. 字符指针和字符数组的使用注意要点


第一个字符串是用数组开辟的,是可以改变的变量。而第二个字符串是一个常量,也是不可改变的值。ptr只是一个指针,是指向这个常量的值,是不能通过这个指针是改变这个常量的。

string.h和cstring都是string类的头文件。这两个头文件主要C语言风格字符串操作的一些方法。

2. String类的书写


ANSWER
code

//普通构造函数 :先判断字符串是否为空,空就给它申请一个\0的位置,否则,就按照传入的字符串的长度,申请一段空间,进行strcpy
String::String(const char* str)//普通构造函数 
{if(str==nullptr)//注意,若传入的是个空字符串,就要给该字符加上字符结束符 {m_data = new char[1];m_data='\0';}else{int length = strlen(str);m_data = new char[length+1];strcpy(m_data,str);}} //析构函数 :要先判断是否为空,然后再删除该对象
String::~String()//析构函数 
{if(m_data)//需要先判断字符串是否为空 {delete m_data;m_data= 0;}} 
// 拷贝构造函数:完成基于同一类的其他对象的构建及初始化。将传入这个对象的值赋值给当前类的成员对象。
String::String(const String* other)//拷贝构造函数 
{if(!other.m_data){m_data = 0;}m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);} 
//赋值函数:首先判断两个对象是不相等的。其次,判断要赋值的对象是否为空,若为空,则直接赋0即可。若不为空,则获得要传入的对象的长度,然后进行拷贝即可。
String &String::operator=(const String& other)//赋值函数 
{if(this!=&other)//若传入的参数内容与本身不一致,才需要进行赋值操作 {delete [] m_data;if(!other.m_data){m_data = 0;}else{m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}} 
}//连接函数:判断要连接的函数是否为空,若为空,则直接返回*this,否则,再判断本身的成员变量是否为空,接下来,才是进行拷贝两者合一的时候。
String& String::operator+(const String &other)
{String newString;if(!other.m_data){newString = *this;}else if(!m_data){newString = other;} else{newString.m_data = new char[strlen(other.m_data)+strlen(m_data)+1];strcpy(newString.m_data,m_data);strcpy(newString.m_data,other.m_data); } return newString;} 
//判断相等函数:判断是否相等,则先判断长度是否一致。然后,再用strcmp判断每一个字符是否一致。
bool String::operator==(const String &other)//判断相等函数 
{if(strlen(m_data)!=strlen(other.m_data)){return false;}else{return strcmp(m_data,other.m_data)?false:true;}
}int String::getLength() 
{return strlen(m_data);
}

3. C++字符串与C字符串的相互转换

c+±>c:

  1. data():以字符数组的形式返回字符串内容,但并不添加’\0’ .
  2. c_str():返回一个以’\0’结尾的字符数组。
  3. copy():则把字符串的内容复制或写入既有的c_string()或字符数组内。

注:C++字符串并不以’\0’结尾

4. string 转int的方法

第7章:网络IO模型

1. select 函数


例子:使用select 函数循环读取键盘输入

#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>int main()
{int keyboard;int ret,i;char c;fd_set readfd;struct timeval timeout;keyboard = open("/dev/tty",O_RDONLY|O_NONBLOCK);//   /dev/tty  表示的是当前终端assert(keyboard>0);while(1){timeout.tv_sec = 1;//这个timeout的作用就是说在1s的时间内,如果没有接收到任何信号,则返回值为0timeout.tv_usec = 0;FD_ZERO(&readfd);FD_SET(keyboard,&readfd);//把打开的终端描述符加入到可读描述符的集合中ret = select(keyboard+1,&readfd,NULL,NULL,&timeout);//writefd  errorfd传入值都为NULL,表示不关心这两种状态if(ret==-1)perror("select error");else if(ret){if(FD_ISSET(keyboard,&readfd)){i = read(keyboard,&c,1);if('\n'==c)continue;printf("The input is %c\n",c);if('q'==c)break;}}     else if(ret==0)printf("time out\n");    			}return 0;
}

总结

待更~

参考

  1. 后台开发核心技术与应用实践

后台开发核心技术与应用实践——书籍阅读笔记

目录

  • 前言
  • 正文
    • 第三章——常见STL的使用
      • 1. 字符指针和字符数组的使用注意要点
      • 2. String类的书写
      • 3. C++字符串与C字符串的相互转换
      • 4. string 转int的方法
    • 第7章:网络IO模型
      • 1. select 函数
  • 总结
  • 参考

前言

这本书在挺多地方都看到推荐了,那就花点时间看一下,看一下人家总结的思路,在前人的基础上继续往前走。

正文

第三章——常见STL的使用

1. 字符指针和字符数组的使用注意要点


第一个字符串是用数组开辟的,是可以改变的变量。而第二个字符串是一个常量,也是不可改变的值。ptr只是一个指针,是指向这个常量的值,是不能通过这个指针是改变这个常量的。

string.h和cstring都是string类的头文件。这两个头文件主要C语言风格字符串操作的一些方法。

2. String类的书写


ANSWER
code

//普通构造函数 :先判断字符串是否为空,空就给它申请一个\0的位置,否则,就按照传入的字符串的长度,申请一段空间,进行strcpy
String::String(const char* str)//普通构造函数 
{if(str==nullptr)//注意,若传入的是个空字符串,就要给该字符加上字符结束符 {m_data = new char[1];m_data='\0';}else{int length = strlen(str);m_data = new char[length+1];strcpy(m_data,str);}} //析构函数 :要先判断是否为空,然后再删除该对象
String::~String()//析构函数 
{if(m_data)//需要先判断字符串是否为空 {delete m_data;m_data= 0;}} 
// 拷贝构造函数:完成基于同一类的其他对象的构建及初始化。将传入这个对象的值赋值给当前类的成员对象。
String::String(const String* other)//拷贝构造函数 
{if(!other.m_data){m_data = 0;}m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);} 
//赋值函数:首先判断两个对象是不相等的。其次,判断要赋值的对象是否为空,若为空,则直接赋0即可。若不为空,则获得要传入的对象的长度,然后进行拷贝即可。
String &String::operator=(const String& other)//赋值函数 
{if(this!=&other)//若传入的参数内容与本身不一致,才需要进行赋值操作 {delete [] m_data;if(!other.m_data){m_data = 0;}else{m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}} 
}//连接函数:判断要连接的函数是否为空,若为空,则直接返回*this,否则,再判断本身的成员变量是否为空,接下来,才是进行拷贝两者合一的时候。
String& String::operator+(const String &other)
{String newString;if(!other.m_data){newString = *this;}else if(!m_data){newString = other;} else{newString.m_data = new char[strlen(other.m_data)+strlen(m_data)+1];strcpy(newString.m_data,m_data);strcpy(newString.m_data,other.m_data); } return newString;} 
//判断相等函数:判断是否相等,则先判断长度是否一致。然后,再用strcmp判断每一个字符是否一致。
bool String::operator==(const String &other)//判断相等函数 
{if(strlen(m_data)!=strlen(other.m_data)){return false;}else{return strcmp(m_data,other.m_data)?false:true;}
}int String::getLength() 
{return strlen(m_data);
}

3. C++字符串与C字符串的相互转换

c+±>c:

  1. data():以字符数组的形式返回字符串内容,但并不添加’\0’ .
  2. c_str():返回一个以’\0’结尾的字符数组。
  3. copy():则把字符串的内容复制或写入既有的c_string()或字符数组内。

注:C++字符串并不以’\0’结尾

4. string 转int的方法

第7章:网络IO模型

1. select 函数


例子:使用select 函数循环读取键盘输入

#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>int main()
{int keyboard;int ret,i;char c;fd_set readfd;struct timeval timeout;keyboard = open("/dev/tty",O_RDONLY|O_NONBLOCK);//   /dev/tty  表示的是当前终端assert(keyboard>0);while(1){timeout.tv_sec = 1;//这个timeout的作用就是说在1s的时间内,如果没有接收到任何信号,则返回值为0timeout.tv_usec = 0;FD_ZERO(&readfd);FD_SET(keyboard,&readfd);//把打开的终端描述符加入到可读描述符的集合中ret = select(keyboard+1,&readfd,NULL,NULL,&timeout);//writefd  errorfd传入值都为NULL,表示不关心这两种状态if(ret==-1)perror("select error");else if(ret){if(FD_ISSET(keyboard,&readfd)){i = read(keyboard,&c,1);if('\n'==c)continue;printf("The input is %c\n",c);if('q'==c)break;}}     else if(ret==0)printf("time out\n");    			}return 0;
}

总结

待更~

参考

  1. 后台开发核心技术与应用实践
发布评论

评论列表 (0)

  1. 暂无评论