c++ 頭文件<cwchar>中常見(jiàn)函數(shù)的實(shí)現(xiàn)代碼
c++ 頭文件<cwchar>中常見(jiàn)函數(shù)的實(shí)現(xiàn)!?。?/h2>
廢話(huà)不多說(shuō)了,代碼說(shuō)明了一切?。。。ùa封裝于名字空間mystd中)
#pragma once #ifndef MYSTD_CWCHAR_H #define MYSTD_CWCHAR_H #include<cassert> // assert #include<cstddef> //std::size_t // 在vs2012中調(diào)試通過(guò),封裝于名字空間mystd中 //文件建議命名為"cwchar.h"(與標(biāo)準(zhǔn)庫(kù)并不沖突) #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--) // 倒序復(fù)制 *--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) // 輔助函數(shù),內(nèi)部使用 { 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; //無(wú)匹配的字符 } 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); // 轉(zhuǎn)調(diào) } 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
下面是一個(gè)簡(jiǎn)單的測(cè)試程序
#include<iostream> #include"cwchar.h" #include<cwchar> #define STD mystd // 改為std調(diào)用標(biāo)準(zhǔn)庫(kù)版本 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++ 常用數(shù)學(xué)函數(shù)詳解匯總#include<math.h>
前言
在實(shí)際應(yīng)用開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)用到一些數(shù)學(xué)計(jì)算。
本文記錄了C++程序開(kāi)發(fā)過(guò)程中常用的數(shù)學(xué)函數(shù),供參考。
一、頭文件
1.cmath
標(biāo)準(zhǔn)C++推薦使用的庫(kù)
#include <cmath>
2.math.h
C語(yǔ)言中的庫(kù),推薦使用該頭文件(使用cmath如果沒(méi)有C++對(duì)應(yīng)的庫(kù)會(huì)出錯(cuò))
#include <math.h>
二、常用函數(shù)
1.開(kāi)平方
double sqrt(double x);
2.求常數(shù)e的x次方
double exp(double x);
3.求常數(shù)x的y次方
double pow(double x, double y);
4.求對(duì)數(shù)lnx、lgx
double log(double x);//求對(duì)數(shù)lnx double log10(double x);//求對(duì)數(shù)lgx
5.求x絕對(duì)值
int abs(x);//整數(shù)型 double fabs(double x);//浮點(diǎn)型
6.取整函數(shù)
double ceil(double x);//向上取整 返回的是大于或等于x的最小整數(shù) double floor(double x);//向下取整 返回的是小于或等于x的最大整數(shù) double fix(double x);//朝零方向取整 double round(double x);//四舍五入到最近的整數(shù)
7.產(chǎn)生隨機(jī)數(shù)
int rand(void); int r=rand()%x+y;//生產(chǎn)一個(gè)在[y,y+x)區(qū)間內(nèi)的數(shù)
8.取整與取余
double modf (double value, double* iptr);//將參數(shù)的整數(shù)部分通過(guò)指針回傳 double fmod (double x, double y);//返回兩參數(shù)相除的余數(shù)
9.三角函數(shù)
double sin(double x);//正弦 double cos(double x);//余弦 double tan(double x);//正切
10.反三角函數(shù)
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);
總結(jié)
以上是C++編程中常用的數(shù)學(xué)函數(shù)匯總,除此之外C++標(biāo)準(zhǔn)模板庫(kù)algorithm中包含了很多的函數(shù)方法,下次我們?cè)賲R總。
到此這篇關(guān)于c++ 頭文件<cwchar>中常見(jiàn)函數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)c++ 頭文件<cwchar>內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言入門(mén)篇--函數(shù)及數(shù)組用法
本篇文章是c語(yǔ)言基礎(chǔ)篇,主要為大家介紹了C語(yǔ)言的函數(shù)與數(shù)組,每個(gè)函數(shù)本質(zhì)上都實(shí)現(xiàn)一個(gè)最小的功能,而main函數(shù)只負(fù)責(zé)調(diào)用函數(shù),實(shí)現(xiàn)代碼的核心邏輯,提高代碼的可維護(hù)性2021-08-08C++之IO類(lèi),文件輸入輸出,string流練習(xí)題
這篇文章主要介紹了C++實(shí)現(xiàn)IO類(lèi)的幾道數(shù)組練習(xí)題,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09C語(yǔ)言模擬實(shí)現(xiàn)strstr函數(shù)的示例代碼
strstr是C語(yǔ)言中的函數(shù),作用是返回字符串中首次出現(xiàn)子串的地址。本文將用C語(yǔ)言模擬實(shí)現(xiàn)strstr函數(shù),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-07-07