自编String类型(C++)

2024-05-03 15:52
文章标签 c++ 类型 string 自编

本文主要是介绍自编String类型(C++),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

设计思路:

  • 首先自己编写了左移运算符的重载,方便后续测试时输出该String类
    • 对于to_lower_case函数,遍历字符串,并修改所有大写字母。
    • 对于to_int函数,从头向尾遍历字符串,将每个数字字符转化为数字,并增加到乘以10倍的num变量上,最后返回num。
    • 对于remove_spaces函数,遍历字符串,维护两个指针,指针i遍历字符串,对于所有非空格字符串,将str[i]赋值给str[j],并执行j++操作。
    • 对于find_replace_str函数,构建循环结构。对于每一次循环,调用strstr找到子串的位置,调用memmove函数对源字符串中字串后的部分进行平移,预留出能够放下替代串的长度,之后调用memcpy函数将替代串拷贝进如源字符串。
    • 对于substring函数可以首先用p指针找到start的位置,然后调用strncpy函数
    • 对于is_substring函数可以调用<cstring>库中的strstr函数判断。

 代码实现:
 

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class String
{friend ostream& operator<<(ostream& cout,String& s);char* str;
public:String(){str=NULL;}String(const char* p){str=new char(strlen(p)+1);strcpy(str,p);}~String(){delete[] str;str=NULL;}int length(){return strlen(str);}char &char_at(int i){if(i<0||i>=strlen(str)){cout<<"超出字符串范围!"<<endl;exit(-1);}return str[i];}const char* get_str(){return str;}String &copy(const char* p){delete []str;str=new char(strlen(p)+1);strcpy(str,p);return *this;}String &copy(const String& s){return copy(s.str);}String &append(const char* p){char* p1=new char(strlen(str)+strlen(p)+1);strcpy(p1,str);strcat(p1,p);delete []str;str=p1;return *this;}String &append(const String& s){return append(s.str);}int compare(const char* p){return strcmp(str,p);}int compare(const String& s){return strcmp(str,s.str);}bool is_substring(const char* sub_str){return strstr(str,sub_str)!=NULL;}bool is_substring(const String& s){return strstr(str,s.str)!=NULL;}String substring(int start,int len){String s;if(start<0||len<=0||start>=strlen(str))return s;char* p=str;for(int i=0;i<start;i++)p++;s.str=new char(len);strncpy(s.str,p,len);return s;}int find_replace_str(const char* find_str,const char* replace_str){int f_len=strlen(find_str);int r_len=strlen(replace_str);int Count=0;char* pos=str;while((pos=strstr(pos,find_str))!=nullptr){memmove(pos+r_len,pos+f_len,strlen(pos+f_len)+1);memcpy(pos,replace_str,strlen(replace_str));pos+=r_len;Count++;}return Count;}void remove_spaces(){int l=strlen(str);int j=0;for(int i=0;i<l;i++){if(str[i]!=' ')str[j++]=str[i];}str[j]='\0';}int to_int(){int sum=0;int l=strlen(str);for(int i=0;i<l;i++){if(str[i]>='0'&&str[i]<='9'){sum*=10;sum+=str[i]-'0';}}return sum;}void to_lower_case(){int l=strlen(str);for(int i=0;i<l;i++){if(str[i]>='A'&&str[i]<='Z'){str[i]='a'+str[i]-'A';}}}
};
ostream& operator<<(ostream& cout,String& s)
{cout<<s.str;return cout;
}
void test1();
void test2();
int main()
{test2();return 0;
}
void test1()
{String s1;String s2("abc DEFG abc");s1.copy("xyz");String s3=("abc");cout<<"s1: "<<s1<<endl;cout<<"s2: "<<s2<<endl;cout<<"s3: "<<s3<<endl;if(s2.is_substring(s1))cout<<"s1 is s2's substring"<<endl;else cout<<"s1 is not s2's substring"<<endl;if(s2.is_substring(s3))cout<<"s3 is s2's substring"<<endl;else cout<<"s3 is not s2's substring"<<endl;String s4=s2.substring(0,3);cout<<"s2.substring(0,3): "<<s4<<endl;int num=s2.find_replace_str("abc","xyz");cout<<"num of abc in s2:"<<num<<endl;cout<<"s2 replace abc with xyz: "<<s2<<endl;s2.remove_spaces();cout<<"s2 without spaces: "<<s2<<endl;s2.to_lower_case();cout<<"s2 to lower case: "<<s2<<endl;s4.copy("123");cout<<"s4: "<<s4<<" to int:"<<s4.to_int()<<endl;
}
void test2()
{String s0=("abc efgh abc");s0.find_replace_str("ab","ijk");cout<<s0<<endl;
}

这篇关于自编String类型(C++)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/957096

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C# WebAPI的几种返回类型方式

《C#WebAPI的几种返回类型方式》本文主要介绍了C#WebAPI的几种返回类型方式,包括直接返回指定类型、返回IActionResult实例和返回ActionResult,文中通过示例代码介绍的... 目录创建 Controller 和 Model 类在 Action 中返回 指定类型在 Action

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js