Microsoft PowerPoint - string_kruse [兼容模式]

Similar documents
Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx

C/C++语言 - C/C++数据

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

新版 明解C++入門編

c_cpp

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

C++ 程式設計

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft PowerPoint - 4. 数组和字符串Arrays and Strings.ppt [兼容模式]

第3章.doc

ebook39-5

FY.DOC

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

全国计算机技术与软件专业技术资格(水平)考试

Microsoft Word - 01.DOC

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

概述

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

INTRODUCTION TO COM.DOC

untitled

NOWOER.OM m/n m/=n m/n m%=n m%n m%=n m%n m/=n 4. enum string x1, x2, x3=10, x4, x5, x; 函数外部问 x 等于什么? 随机值 5. unsigned char *p1; unsigned long *p

Strings

Microsoft Word cppFinalSolution.doc

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

ebook39-6

Strings

Microsoft Word - ch04三校.doc

Microsoft PowerPoint - 10 模板 Template.pptx

untitled

提问袁小兵:

Microsoft Word - 物件導向編程精要.doc

科学计算的语言-FORTRAN95

untitled

CC213

untitled

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

untitled

ebook50-11

Microsoft Word - Learn Objective-C.doc

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

2/80 2

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word - template.doc

csg(1_29)cs.p65

mvc

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

Guide to Install SATA Hard Disks

Microsoft Word - Final Exam Review Packet.docx

穨control.PDF

JavaIO.PDF

Cover-CsG.65Cs

PowerPoint Presentation

Chapter12 Derived Classes

Simulator By SunLingxi 2003

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

VASP应用运行优化

念「舊」新潮流─國立臺灣圖書館舊籍行銷策略探究

北京大学

Microsoft PowerPoint - Lecture7II.ppt

软件测试(TA07)第一学期考试

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

epub83-1

Microsoft Word - CPE考生使用手冊 docx

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

NethersoleJO89(8).indd

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

Microsoft Word - MSP430 Launchpad 指导书.docx

Open topic Bellman-Ford算法与负环

致 谢 本 人 自 2008 年 6 月 从 上 海 外 国 语 大 学 毕 业 之 后, 于 2010 年 3 月 再 次 进 入 上 外, 非 常 有 幸 成 为 汉 语 国 际 教 育 专 业 的 研 究 生 回 顾 三 年 以 来 的 学 习 和 生 活, 顿 时 感 觉 这 段 时 间 也

运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2

Microsoft Word doc

新・明解C言語入門編『索引』

Chapter 9: Objects and Classes

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

SP_ SP_03 JAVA...6 SP_10 SQL...8 SP_ SP_ SP_ SP_ SP_ SP_ SP_ SP_04.NET...33 SP_02 C...37 SP_05

Java

《计算概论》课程 第十九讲 C 程序设计语言应用

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

國家圖書館典藏電子全文

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

3.1 num = 3 ch = 'C' 2

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

2005硕士论文模版

CC213

迅速在两个含有大量数据的文件中寻找相同的数据

Microsoft Word - 口試本封面.doc

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

EC51/52 GSM /GPRS MODEN

untitled

C 1

Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice

第一章

CH01.indd

USPTO Academic research Corporate needs Global/International Inventors Libraries News Media/Publication Patent Attorney or Agent USPTO e (ebusiness Ce

Transcription:

Strings Strings in C not encapsulated Every C-string has type char *. Hence, a C-string references an address in memory, the first of a contiguous set of bytes that store the characters making up the string. The storage occupied by the string must terminate with the special character value \0. The standard header file <cstring> (or <string.h>) contains a library of functions that manipulate C-strings. some important functions: char* strcpy(char* to, char* from); int strcmp(char* one, char* two); char* strcat(char* to, char* from); int strlen(char* str);

Strings Strings in C++ encapsulated In C++, the output operator << is overloaded to apply to cstrings, so that a simple instruction cout << s prints the string s. In C++, it is easy to use encapsulation to embed C- strings into safer class-based implementations of strings. The standard template library includes a safe string implementation in the header file <string>. This library implements a class called std :: string that is convenient, safe, and efficient. #include <string> using namespace std;

Strings implementation class specification: class String{ public: (1)String(); // 构造函数 (2)~String();// 析构函数 (3)String(const String &copy); // 拷贝构造函数 (4)String(const char* copy); // 将 C 字符串转成 C++ 中的串 (5)String(List<char> &copy);// 将 List 转成 C++ 串 (6)void operator=(const String &copy); // 赋值符号重载 (7)const char* c_str() const;// 转成 C 中的字符串, 为什么需 要这个方法? protected: char* entries; int length; ; 4 String s( some_string ); String s; S= some_string ; String s= some_string ; 1 4 6 4 7 const char *new_s=s.c_str();

Strings Strings in C++ 全局重载操作符 bool operator==(const String& first, const String& second); bool operator>(const String& first, const String& second); bool operator<(const String& first, const String& second); bool operator>=(const String& first, const String& second); bool operator<=(const String& first, const String& second); bool operator!=(const String& first, const String& second); 运算符 规则 所有的一元运算符 建议重载为成员函数 = () [] -> 只能重载为成员函数 += -= /= *= &= = ~= %= >>= <<= 建议重载为成员函数 所有其它运算符 建议重载为全局函数

Strings realization of some important methods // 利用 C 的字符串构造 String::String(const char* in_string){ length=strlen(in_string); entries=new char[length+1]; strcpy(entries,in_string); // 利用 List 进行构造 String::String(List<char>& in_list){ length=in_list.size(); entries=new char[length+1]; for (int i=0;i<length;i++) in_list.retrieve(i,entries[i]); entries[length]= \0 ;

Strings realization of some important methods // 转成 C 中的字符串 const char* String::c_str() const { return (const char*) entries;// 提供到内部 String 数据的访问 // 有什么问题? 是否有更好的办法? String s= abc ; const char *new_string=s.c_str(); s= def ;// 调用赋值重载, 要将 s 的原空间回收! cout<<new_string;

可选的实现 : 为 string 数据的副本分配动态内存 const char* String::c_str() const {int len; char * temp; len=strlen(entries;) temp=new char[len+1]; strcpy(temp,entries); return temp; String s= some very_long string ; cout<<s.c_str(); // 效率低, 特别是字符串很长时 // 客户程序必须记住使用之后要删除它, 否则会因为临时对象没有删除而产生了垃圾!

Strings // 等于等于符号的重载 bool operator= =(const String& first, const String& second) { return (strcmp(first.c_str(),second.c_str())==0);

Further String Operations void strcat(string &add_to, const String &add_on) void strcpy(string &copy, const String &original); void strncpy(string &copy, const String &original, int n); int strstr(const String &text, const String &target); String read_in(istream &input) String read_in(istream &input, int terminator); void write(string &s)

Samples of Further String Operations void strcat(string &add_to, const String &add_on) /* Post: The function concatenates String add on onto the end of String add to.*/ { const char *cfirst = add_to.c_str( ); const char *csecond = add_on.c_str( ); char *copy = new char[strlen(cfirst) + strlen(csecond) +1]; strcpy(copy, cfirst); strcat(copy, csecond); add_to = copy; delete []copy;

int strstr(const String &text, const String &target); /*postcondition: If String target is a substring of String text, the function returns the array index of the first occurrence of the string stored in target in the string stored in text. else: The function returns a code of -1.*/ { int answer; const char * content_s = text.c_str( ); char *p = strstr((char *) content_s, targer.c_str( )); if (p == NULL) answer = -1; else answer = p- content_s; return answer;

String read_in(istream &input) /* Post: Return a String read (as characters terminated by a newline or an end-of-file character) from an istream parameter. */ { List<char> temp; int size = 0; char c; while ((c = input.peek( ))!= EOF && (c = input.get( ))!= \n ) temp.insert(size++, c); String answer(temp); return answer;

We shall also find it useful to apply the following String output function as an alternative to the operator <<. void write(string &s) /* Post: The String parameter s is written to cout. */ { cout << s.c_str( ) << endl;

实验题目 1 实验五 list 类的实现和应用 2 string 类的实现和测试 ( 选做 )