c++ 头文件<cwchar>中常见函数的实现代码

 更新时间:2023年12月14日 11:27:32   作者:爱读庄子的码农  
本文记录了c++ 头文件<cwchar>中常见函数的实现,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

c++ 头文件<cwchar>中常见函数的实现!!!

废话不多说了,代码说明了一切!!!(代码封装于名字空间mystd中)

#pragma once 
#ifndef MYSTD_CWCHAR_H
#define MYSTD_CWCHAR_H
#include<cassert> // assert 
#include<cstddef> //std::size_t
// 在vs2012中调试通过,封装于名字空间mystd中
//文件建议命名为"cwchar.h"(与标准库并不冲突)
#define MYSTD_BEGIN namespace mystd {
#define MYSTD_END   }
#ifdef __cplusplus
MYSTD_BEGIN
	typedef std::size_t size_type;
	typedef wchar_t char_type;
	inline size_type wcslen(const char_type* wcs)
	{
     assert(wcs != 0);
	 size_type count = 0;
	 while(*wcs++)
		 ++count;
	 return count;
	}
	inline char_type* wcscat(char_type* destination,const char_type *source)
	{
		assert(destination != 0 && source != 0);
		char_type *des = destination + mystd::wcslen(destination);
		while(*des++ = *source++);
		return destination;
	}
	inline char_type* wcsncat(char_type* destination,const char_type *source,size_type num)
	{
		assert(destination != 0 && source != 0);
		char_type *des = destination + mystd::wcslen(destination);
		while(num-- && *source)
			*des++ = *source++;
		*des = 0; 
		return destination;
	}
	inline char_type* wcscpy(char_type *destination,const char_type *source)
	{
		assert(destination != 0 && source != 0);
		char_type *des = destination;
		while(*des++ = *source++);
		return destination;
	}
	inline char_type* wcsncpy(char_type *destination,const char_type *source,size_type num)
	{
		assert(destination != 0 && source != 0);
		char_type *des = destination;
		while(num--)
			*des++ = *source++; 
		return destination; // 可能不包含null wide character 
	}
	inline int wcscmp(const char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		while(*wcs1 && *wcs1 == *wcs2)
			++wcs1, ++wcs2;
		return *wcs1 - *wcs2;
	}
	inline int wcsncmp(const char_type *wcs1,const char_type *wcs2,size_type num)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		while(num-- && *wcs1 && *wcs1 == *wcs2)
			++wcs1, ++wcs2;
		if(num == size_type(-1))  // 包含了num == 0的情况
			return 0;
		else
			return *wcs1 - *wcs2;
	}
	inline const char_type* wmemchr(const char_type* pointer,char_type val,size_type num)
	{
    	assert(pointer != 0);
		char_type *ptr = (char_type*)pointer;
	    for(size_type i = 0; i < num; ++i)
		{
			if(*ptr == val)
				break;
			++ptr;
		}
		return ptr;
	}
    inline char_type* wmemchr(char_type* pointer,char_type val,size_type num)
	{
		assert(pointer != 0);
		return (char_type*)wmemchr((const char_type*)pointer,val,num);
	}
	inline int wmemcmp(const char_type *ptr_1,const char_type *ptr_2,size_type num)
	{
		assert(ptr_1 != 0 && ptr_2 != 0);
		while(num-- && *ptr_1 == *ptr_2)
			++ptr_1, ++ptr_2;
		if(num == size_type(-1))
			return 0;
		else
			return *ptr_1 - *ptr_2;
	}
	inline char_type* wmemset(char_type *pointer,char_type val,size_type num)
	{
		assert(pointer != 0);
		char_type *ptr = pointer;
		while(num--)
			*ptr++ = val;
		return pointer;
	}
	inline char_type* wmemmove(char_type *destination,const char_type *source,size_type num)
	{
		assert(destination != 0 && source != 0); 
		if(destination == source || num == 0)
			return destination;
		char_type *des = (char_type*)destination;  
		const char_type *src = (char_type*)source;  
		if(des < src || des >= src + num)  
		{
				while(num--)
					*des++ = *src++;
				return destination;
		}
		des += num;
		src += num;
		while(num--) // 倒序复制
			*--des = *--src;
		return destination;
	}
	inline char_type* wmemcpy(char_type *destination,const char_type *source,size_type num)
	{
		assert(destination != 0 && source != 0);
		return mystd::wmemmove(destination,source,num);
	}
	inline bool w_is_inside(const char_type *wcs,char_type val) // 辅助函数,内部使用
	{
		assert(wcs != 0);
		while(*wcs)
		{
			if(*wcs == val)
				return true;
			else 
				++wcs;
		}
		return false;
	}
	inline size_type wcsspn(const char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		size_type count = 0;
		while(*wcs1 && w_is_inside(wcs2,*wcs1))
			++count, ++wcs1;
		return count;
	}
	inline size_type wcscspn(const char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		size_type count = 0;
		while(*wcs1 && !w_is_inside(wcs2,*wcs1))
			++count, ++wcs1;
		return count;
	}
	inline const char_type* wcsstr(const char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		size_type len_1 = mystd::wcslen(wcs1);
		size_type len_2 = mystd::wcslen(wcs2);
		if(len_1 < len_2) 
			return 0;
		const char_type *search_last = wcs1 + (len_1 - len_2);
		while(wcs1 <= search_last)
		{
			if(mystd::wcsncmp(wcs1,wcs2,len_2) == 0)
				return wcs1;
			else
				++wcs1;
		}
		return 0;
	}
    inline char_type* wcsstr(char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		return (char_type*)mystd::wcsstr((const char_type*)wcs1,wcs2);
	}
	inline const char_type* wcschr(const char_type *wcs,char_type val)
	{
		assert(wcs != 0);
		while(*wcs && *wcs != val)
			++wcs;
		if(*wcs)
			return wcs;
		else
			return 0;
	}
	inline char_type* wcschr(char_type *wcs,char_type val)
	{
		assert(wcs != 0);
		return (char_type*)mystd::wcschr((const char_type*)wcs,val);
	}
	inline const char_type* wcsrchr(const char_type *wcs,char_type val)
	{ // val可能为null wide character 
		assert(wcs != 0);
		size_type len = mystd::wcslen(wcs);
		const char_type *ptr = wcs + len;
		if(val == 0)
			return ptr;
		--ptr;
		while(len--)
			if(*ptr == val)
				return ptr;
			else
				--ptr;
		return 0;  //无匹配的字符
	}
	inline char_type* wcsrchr(char_type *wcs,char_type val)
	{  //val可能为null wide character 
		assert(wcs != 0);
		return (char_type*)mystd::wcsrchr((const char_type*)wcs,val); // 转调
	}
	inline const char_type* wcspbrk(const char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		while(*wcs1 && !w_is_inside(wcs2,*wcs1))
			++wcs1;
		if(*wcs1 == 0)
			return 0;
		else
			return wcs1;
	}
	inline char_type* wcspbrk(char_type *wcs1,const char_type *wcs2)
	{
		assert(wcs1 != 0 && wcs2 != 0);
		return (char_type*)mystd::wcspbrk((const char_type*)wcs1,wcs2);
	}
MYSTD_END // end of namespace mystd 
#endif // __cplusplus
#endif // MYSTD_CWCHAR_H

下面是一个简单的测试程序

#include<iostream>
#include"cwchar.h"
#include<cwchar>
#define STD mystd // 改为std调用标准库版本
using std::endl;
using std::cout;
int main()
{
	wchar_t buf[100];
	STD::wmemcpy(buf,L"hello world",sizeof(buf) / sizeof(wchar_t));
	unsigned count = STD::wcslen(buf);
	for(unsigned i = 0; i < count; ++i)
		cout<<(char)buf[i];
	cout<<endl;
	system("pause");
	return 0;
}

C++ 常用数学函数详解汇总#include<math.h>

前言

在实际应用开发过程中,经常会用到一些数学计算。

本文记录了C++程序开发过程中常用的数学函数,供参考。

一、头文件

1.cmath

标准C++推荐使用的库

#include <cmath>

2.math.h

C语言中的库,推荐使用该头文件(使用cmath如果没有C++对应的库会出错)

#include <math.h>

二、常用函数

1.开平方

double sqrt(double x);

2.求常数e的x次方

double exp(double x);

3.求常数x的y次方

double pow(double x, double y);

4.求对数lnx、lgx 

double log(double x);//求对数lnx
double log10(double x);//求对数lgx 

5.求x绝对值

int abs(x);//整数型
double fabs(double x);//浮点型

6.取整函数

double ceil(double x);//向上取整 返回的是大于或等于x的最小整数
double floor(double x);//向下取整 返回的是小于或等于x的最大整数
double fix(double x);//朝零方向取整
double round(double x);//四舍五入到最近的整数

7.产生随机数

int rand(void);
int r=rand()%x+y;//生产一个在[y,y+x)区间内的数

8.取整与取余

double modf (double value, double* iptr);//将参数的整数部分通过指针回传
double fmod (double x, double y);//返回两参数相除的余数

9.三角函数

double sin(double x);//正弦
double cos(double x);//余弦
double tan(double x);//正切

10.反三角函数

double asin(double x);//反正弦 [−π/2, π/2]
double acos(double x);//反余弦 [0, π]
double atan(double x);//反正切(主值)   [−π/2, π/2]
double atan2(double x);//反正切(整圆值) [−π, π]

11.π的表示

const double pi = acos(-1.0);

总结

以上是C++编程中常用的数学函数汇总,除此之外C++标准模板库algorithm中包含了很多的函数方法,下次我们再汇总。

到此这篇关于c++ 头文件&lt;cwchar&gt;中常见函数的实现的文章就介绍到这了,更多相关c++ 头文件&lt;cwchar&gt;内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言入门篇--函数及数组用法

    C语言入门篇--函数及数组用法

    本篇文章是c语言基础篇,主要为大家介绍了C语言的函数与数组,每个函数本质上都实现一个最小的功能,而main函数只负责调用函数,实现代码的核心逻辑,提高代码的可维护性
    2021-08-08
  • C++之IO类,文件输入输出,string流练习题

    C++之IO类,文件输入输出,string流练习题

    这篇文章主要介绍了C++实现IO类的几道数组练习题,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • 你知道C语言中#和##表示的意义吗

    你知道C语言中#和##表示的意义吗

    如标题,这篇文章会讲解C语言中的#和##是啥意思。我相信,大部分朋友应该都没怎么用过,这两个玩意的使用条件也相当苛刻,快跟随小编一起来看看吧
    2023-04-04
  • C++小游戏BrickHit实例代码

    C++小游戏BrickHit实例代码

    本文通过实例代码给大家介绍了C++小游戏BrickHit的相关资料,需要的朋友可以参考下
    2018-02-02
  • C语言模拟实现strstr函数的示例代码

    C语言模拟实现strstr函数的示例代码

    strstr是C语言中的函数,作用是返回字符串中首次出现子串的地址。本文将用C语言模拟实现strstr函数,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-07-07
  • C语言/C++如何生成随机数

    C语言/C++如何生成随机数

    这篇文章主要介绍了C语言/C++如何生成随机数,C语言/C++产生随机数主要用到的是rand()函数, srand()函数,C语言/C++里没有自带的random(int number)函数,如何解决?感兴趣的小伙伴们可以参考一下
    2016-04-04
  • C++实现图像压缩的示例代码

    C++实现图像压缩的示例代码

    这篇文章主要为大家详细介绍了如何使用C++实现图像压缩的功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • C++ 详细讲解对象的构造顺序

    C++ 详细讲解对象的构造顺序

    对象的构造往往和构造函数会牵扯在一起,构造函数的函数可能会由非常复杂的逻辑所组成,不同类的构造函数的程序逻辑很可能是相互依赖的,当这种相互依赖一旦成立,那么对象的构造顺序很可能导致难以调试的Bug出现
    2022-04-04
  • c语言排序之归并排序(递归和非递归)

    c语言排序之归并排序(递归和非递归)

    这篇文章主要介绍了 c语言排序之归并排序(递归和非递归),归并就是把两个或多个序列合并,本文主要介绍二路归并,下文相关资料需要的小伙伴可以参考一下
    2022-04-04
  • 如何区分C++中的inline和#define宏

    如何区分C++中的inline和#define宏

    这篇文章主要介绍了如何区分C++中的inline和#define宏,文中讲解非常详细,代码帮助大家更好的参考和学习,感兴趣的朋友可以了解下
    2020-06-06

最新评论