C Implement a string class with basic functionality
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
// ------String.h------#ifndef STRING_H#define STRING_H#include <iostream>using namespace std;class String{public: String(); String(int n, char c); String(const char* source); String(const String& s); // String& operator = (char* s); String& operator = (const String& s); ~String(); char& operator [] (int i){return a[i];} const char& operator [] (int i) const {return a[i];} // Indexer to constant. String& operator += (const String& s); int length(); friend istream& operator >> (istream& is, String& s); // Try to figure out why set ">>" friend function member. // friend bool operator < (const String& left,const String& right); friend bool operator > (const String& left, const String& right); // These three operators is not necessary to set them friend. Here is just for simplification. friend bool operator == (const String& left, const String& right); friend bool operator != (const String& left, const String& right);private: char* a; int size;};#endif
// ------String.cpp------#include "String.h"#include <cstring>#include <cstdlib>String::String(){ a = new char[1]; a[0] = '\0'; size = 0;}String::String(int n, char c){ a = new char[n + 1]; memset(a, c, n); a[n] = '\0'; size = n;}String::String(const char* source){ if(source == NULL) { a = new char[1]; a[0] = '\0'; size = 0; } else { size = strlen(source); a = new char[size + 1]; strcpy(a, source); }}String::String(const String& s){ size = strlen(s.a); a = new char[size + 1]; strcpy(a,s.a);}String& String::operator = (const String& s){ if(this == &s) return *this; else { delete[] a; size = strlen(s.a); a = new char[size + 1]; strcpy(a, s.a); return *this; }}String::~String(){ delete[] a;}String& String::operator += (const String& s){ int j = strlen(a); int size = j + strlen(s.a); char* tmp = new char[size + 1]; strcpy(tmp, a); strcpy(tmp + j, s.a); delete[] a; a = tmp; return *this;}int String::length(){ return strlen(a);}
// ------Main.cpp------#include <iostream>#include "String.h"using namespace std;bool operator == (const String& left, const String& right){ int a = strcmp(left.a, right.a); if(a == 0) { return true; } else { return false; }}bool operator != (const String& left, const String& right){ return !(left == right);}ostream& operator << (ostream& os, String& s){ int length = s.length(); for(int i = 0; i < length; i++) { os << s[i]; } return os;}String operator + (const String& a, const String& b){ String temp; temp = a; temp += b; return temp;}bool operator < (const String& left, const String& right){ int j = 0; while((left[j] != '\0') && (right[j] != '\0')) { if(left[j] < right[j]) { return true; } else { if(left[j] == right[j]) { j++; continue; } else { return false; } } } if((left[j] == '\0') && (right[j] != '\0')) { return true; } else { return false; }}bool operator > (const String& left, const String& right){ int a = strcmp(left.a, right.a); if(a > 0) { return true; } else { return false; }}istream& operator >> (istream& is, String& s){ delete[] s.a; s.a = new char[20]; int m = 20; char c; int i = 0; while(is.get(c) && isspace(c)); if(is) { do { s.a[i] = c; i++; if(i == m - 1) { s.a[i] = '\0'; char* b = new char[m]; strcpy(b, s.a); m = m * 2; s.a = new char[m]; strcpy(s.a, b); delete[] b; } } while (is.get(c) && !isspace(c)); if(is) { is.unget(); } } s.size = i; s.a[i] = '\0'; return is;}int main(){ String a = "abcd"; String b = "www"; String c(6, 'l'); String d; String e = a; String f; cin >> f; String g; g = a + b; if(a < b) { cout << "a < b" << endl; } else { cout << "a >= b" << endl; } if(e == a) { cout << "e == a" << endl; } else { cout << "e != a" << endl; } b += a; cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; cout << e << endl; cout << f << endl; cout << g << endl; cout << g[0] << endl; return 0;}// Output:/*ta < be == aabcdwwwabcdllllllabcdtabcdwwwa*/
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
C Implement a string class with basic functionality相关推荐
- 解决“Cradle project sync failed. Basic functionality(e.g.editing.debugging)”will not work properly
把Android studio版本升级之后,再导入原来的项目就会报错,gradle失败,显示"Cradle project sync failed. Basic functionality( ...
- Gradle project sync failed. Basic functionality (e.g. editing, debugging) will not work properly
记录一次bug解决 由于公司更新电脑,重新装了软件环境,所以就安装了最新版AS以及JDK,但是安装完成后运行以前的工程出现了Gradle project sync failed. Basic func ...
- Android Studio报错gradle project sync failed.Basic functionality(e.g.editing,debugging) will not work
昨天刚安装完Android Studio ,坑是真的多 首先你要下载一个sdk 和一个Android studio 安装包 看了好多安装教程博客,感觉这篇还是不错的:https://www.cnblo ...
- QT Basic 014 Model/View programming (模型、视图编程)
前言:本文不是纯文本翻译,加入了对概念的理解,纯文本翻译,请看文后的一个链接. Model/View Programming Introduction to Model/View Programmin ...
- C++ string 使用详解(含C++20新特性)
目录 元素访问 迭代器 容量 操作 查找 std::erase, std::erase_if (std::basic_string) std::basic_string 简介 basic_string ...
- Java Development - String
Java Development - String 1.What 2.Why 3.How 3.1 Creating a java string(创建一个字符串) 3.2 String and char ...
- STL中map和string, vector 用法详解
1. map 用法详解 std map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成 ...
- 转: std::string用法详解
原文地址为: 转: std::string用法详解 C++中的string 类 简单介绍 前言: string 的角色 1 string 使用 1.1 充分使用string 操作符 1.2 眼花缭乱 ...
- OkHttp配置Basic Auth
需求:远程服务使用HTTPS连接,并需要Basic Auth认证 在配置类里面注入一个自定义OkHttpClient @Value("${sign.httpName}")priva ...
最新文章
- Glide源码(基于4.8版本)解析
- 关于Java里面File类创建txt文件重复???
- mysql1300错误什么意思_mysql error 1201-1300错误大全
- JSTL笔记—c标签
- 2015.12.11-2015.12.13 金华旅程的学习计划
- 网络计算机 用户名和密码,局域网需要用户名和密码怎么办
- 财务系统专用服务器中标公告,东南大学财务处服务器存储-招标办公室.doc
- Full_of_Boys训练4总结
- 市民举报邻居去韩国代购归来未隔离,真相很尴尬:表面防疫,实则打假?
- 在vmware server中部署linux redhat 5.4 ORACLE RAC11g +ASM
- 衡算计算机性能指标,广东海洋大学2018年硕士生招生初试科目的考试内容范围说明计算机.PDF...
- 在有向图中找出所有简单环--Johnson算法
- PTA2-6 打印学生选课清单(15 分)
- python语言关键字的是_不属于Python语言关键字的是( )
- python爬去新浪微博_!如何通过python调用新浪微博的API来爬取数据
- 回首神仙地,还道人间好
- android怎么美化ui,安卓教程第一期最终篇(转)systemui.apk最全修改美化
- test_and_set_bit
- 状态码401 和 403
- aspose-words,pdf