下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
size_t split(const char *src,const char *delimiter,strarray &stra) {stra.clear();char *temp = (char*)src;int len_src = strlen(temp);unsigned long pro;VirtualProtect(temp,len_src + 1,0x40,&pro);char *pos_last = temp;char *pos = strstr(temp,delimiter);char chr = '';int ret = 0;int len_delimiter = strlen(delimiter);while(pos){chr = *pos;*pos = '';stra.push_back((const char*)pos_last);*pos = chr;pos_last = pos + len_delimiter;pos = strstr(pos_last,delimiter);++ret;}stra.push_back(pos_last);++ret;VirtualProtect(temp,pro,NulL);return ret;} size_t _split(const char *src,const char delimiter,strarray &stra){stra.clear();char *temp = (char*)src;int len_src = strlen(temp);unsigned long pro;VirtualProtect(temp,&pro);char *pos_last = temp;char *pos = strchr(temp,delimiter);char chr = '';int ret = 0;while(pos){chr = *pos;*pos = '';stra.push_back((const char*)pos_last);*pos = chr;pos_last = pos + 1;pos = strchr(pos_last,NulL);return ret;} //查找s1,s2,返回两者之间的内容,incsym为true时返回内容包括s1,s2 size_t mIDex(const char *src,const char *s1,const char *s2,strarray &stra,bool incsym){stra.clear();char *temp = (char*)src;int len_src = strlen(temp);unsigned long pro;VirtualProtect(temp,&pro);char *pos_left = temp,*pos_right = temp;int len_s1 = strlen(s1),len_s2 = strlen(s2);char chr = '';int ret = 0;while(1){pos_left = strstr(pos_right,s1);if(!pos_left)break;pos_left += len_s1;pos_right = strstr(pos_left,s2);if(!pos_right)break;if(incsym){chr = *(pos_right + len_s2);*(pos_right + len_s2) = '';stra.push_back(pos_left - len_s1);*(pos_right + len_s2) = chr;}else{chr = *pos_right;*pos_right = '';stra.push_back(pos_left);*pos_right = chr;}pos_right += len_s2;++ret;}VirtualProtect(temp,NulL);return ret;} size_t _mIDex(const char *src,const char c1,const char c2,*pos_right = temp;char chr = '';int ret = 0;while(1){pos_left = strchr(pos_right,c1);if(!pos_left)break;pos_right = strchr(pos_left++,c2);if(!pos_right)break;if(incsym){chr = *(pos_right + 1);*(pos_right + 1) = '';stra.push_back(pos_left - 1);*(pos_right + 1) = chr;}else{chr = *pos_right;*pos_right = '';stra.push_back(pos_left);*pos_right = chr;}++pos_right;++ret;}VirtualProtect(temp,NulL);return ret;} size_t _replace(const char chold,const char chNew,std::string &str){int len_src = str.length();char *temp = new char [len_src + 1];strcpy_s(temp,str.c_str());char *pos = temp;size_t nResult = 0;while(1){pos = strchr(pos,chold);if(pos)*pos = chNew;elsebreak;pos += 1;++nResult;}str = temp;delete temp;return nResult; } //这里主要还是用了stl了,不用stl的话,在查找替换位置上,分配动态数组可能会麻烦点 //然后用了std::string += 的运算符重载,自己实现的话,从分配,释放内存上来看,是存在安全隐患的,然后拷贝字符串的代码可能也会麻烦点。 size_t replace(const char *lpold,const char *lpNew,str.c_str());size_t len_old = strlen(lpold);char *pos = temp;std::vector<char*> vRepos;while(1){pos = strstr(pos,lpold);if(pos){*pos = '';vRepos.push_back(pos + len_old);}elsebreak;pos += len_old;}size_t nSize = vRepos.size();str.clear();str = temp;for(size_t nIndex = 0; nIndex < nSize; ++nIndex){str += lpNew;str += vRepos[nIndex];}delete temp;return nSize;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的split, midex, replace 基于C函数库字符串函数的基本实现全部内容,希望文章能够帮你解决split, midex, replace 基于C函数库字符串函数的基本实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)