北京大学

Similar documents
C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

Microsoft PowerPoint - string_kruse [兼容模式]

02

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

C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

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 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

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

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

C/C++程序设计 - 字符串与格式化输入/输出

第3章.doc

新版 明解C++入門編

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

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

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

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

FY.DOC

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

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

北京大学

Microsoft PowerPoint - 10 模板 Template.pptx

C 1

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

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

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

幻灯片 1

C++ 程序设计 OJ4 - 参考答案 MASTER 2019 年 5 月 30 日 1

(A) 二 小 時 (B) 三 小 時 (C) 四 小 時 (D) 五 小 時 第 一 組 出 題 6. 若 對 於 收 到 的 交 通 違 規 罰 單 不 服, 在 收 到 罰 單 幾 日 內 須 向 警 察 機 關 或 監 理 機 關 申 訴? (A) 十 天 (B) 十 五 天 (C) 二 十

Microsoft Word - CPE考生使用手冊 docx

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

C/C++ - 文件IO

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

Microsoft Word - 第3章.doc

Guava学习之CharSequenceReader

c_cpp

C++ 程序设计 OJ4 - 参考答案 MASTER 2019 年 5 月 3 日 1

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

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

untitled

Chapter12 Derived Classes

IO

ebook39-5

}; "P2VTKNvTAnYNwBrqXbgxRSFQs6FTEhNJ", " " string imagedata; if(0!= read_image("a.jpg",imagedata)) { return -1; } string rsp; ytopen_sdk m_sd

Strings

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

一、单项选择题(共20题,每题1.5分,共计30分;每题有且仅有一个正确选项)

Eclipse C C++, or

ebook39-6

Microsoft Word - well_game.doc


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

Microsoft PowerPoint - 3. 函数Functionl.ppt [兼容模式]

C



2

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

untitled

数据结构与算法 - Python基础

OOP with Java 通知 Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢 学习使用文本编辑器 学习使用 cmd: Power shell 阅读参考资料

Microsoft Word - AEL 序.doc

untitled

C++ 程式設計

新・解きながら学ぶJava

概述

《C语言程序设计》教材习题参考答案

untitled

试卷代号 :1075 座位号 rn 国家开放大学 ( 中央广播电视大学 )2015 年秋季学期 " 开放本科 " 期末考试 c+ 十语言程序设计试题 2016 年 1 月 t 问一 Urr-f 斗 士 1 1. 下面的保留字 ( ) 不能作为函数的返回类型 A. void B. int C. new

院 伙 食 补 助 费 4,700 元, 交 通 费 2,000 元, 住 宿 费 1,000 元, 伤 残 赔 偿 金 100,000 元, 精 神 损 害 赔 偿 金 100,000 元 ( 待 司 法 鉴 定 后 重 新 确 认 ); 二 诉 讼 费 鉴 定 费 由 被 告 承 担 被 告 省

试卷代号 ~1075 座位号 E 口 国家开放大学 ( 中央广播电视大学 )20]5 年秋季学期 " 开放本科 " 期末考试 C 十十语言程序设计 试题 同二二十斗 2016 年 1 月 巴叫一 1. 下面的保留字 ( ) 不能作为函数的返回类型 A. void B. int C. new D. l

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

Microsoft PowerPoint - 01_Introduction.ppt

C++11概要 ライブラリ編

程序员-下午题-10下

Microsoft PowerPoint - 5. 指针Pointers.ppt [兼容模式]

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

extend


Microsoft PowerPoint - ds-1.ppt [兼容模式]

CHAPTER VC#

Guava学习之Resources

《C语言程序设计》第2版教材习题参考答案

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

untitled

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

Microsoft PowerPoint - CPP-Ch Print.ppt [兼容模式]

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式]

14. 阿 亮 在 寒 假 春 節 期 間 與 父 母 到 一 座 廟 裡 拜 拜, 廟 裡 的 神 有 掌 生 死 簿 的 判 官 勾 攝 生 魂 的 黑 白 無 常 執 行 拘 提 魂 魄 的 牛 頭 馬 面, 整 間 廟 看 起 來 有 些 陰 森, 請 問 阿 亮 到 了 哪 一 座 廟 內

上海交通大学

untitled

Microsoft Word - 魔术师

Microsoft PowerPoint - 1. C++介绍.ppt [兼容模式]

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

PowerPoint 演示文稿

( 一 ) 全 面 贯 彻 党 和 国 家 的 教 育 方 针 政 策, 落 实 国 家 有 关 教 育 的 法 律 法 规 ; 研 究 草 拟 江 苏 省 教 育 法 规 和 政 策, 并 组 织 实 施 ( 二 ) 研 究 教 育 发 展 战 略 思 路, 统 筹 规 划 协 调 指 导 江 苏

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

chap07.key

C6_ppt.PDF

Microsoft Word - temp71.doc

PowerPoint Presentation

Transcription:

1 string 类 郭炜刘家瑛 北京大学程序设计实习

string 类 string 类是一个模板类, 它的定义如下 : typedef basic_string<char> string; 使用 string 类要包含头文件 <string> string 对象的初始化 : string s1("hello"); // 一个参数的构造函数 string s2(8, x ); // 两个参数的构造函数 string month = March ; 2

string 类 不提供以字符和整数为参数的构造函数错误的初始化方法 : string error1 = c ; // 错 string error2( u ); // 错 string error3 = 22; // 错 string error4(8); // 错 可以将字符赋值给 string 对象 string s; s = n ; 3

string 类程序样例 #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[ ]){ string s1("hello"); cout << s1 << endl; string s2(8, 'x'); cout << s2 << endl; string month = "March"; cout << month << endl; string s; s='n'; cout << s << endl; return 0; } 程序输出 : Hello xxxxxxxx March n 4

string 类 构造的 string 太长而无法表达 会抛出 length_error 异常 string 对象的长度用成员函数 length() 读取 ; string s("hello"); cout << s.length() << endl; string 支持流读取运算符 string stringobject; cin >> stringobject; string 支持 getline 函数 string s; getline(cin, s); 5

string 的赋值和连接 用 = 赋值 string s1("cat"), s2; s2 = s1; 用 assign 成员函数复制 string s1("cat"), s3; s3.assign(s1); 用 assign 成员函数部分复制 string s1("catpig"), s3; s3.assign(s1, 1, 3); // 从 s1 中下标为 1 的字符开始复制 3 个字符给 s3 6

string 的赋值和连接 单个字符复制 s2[5] = s1[3] = a ; 逐个访问 string 对象中的字符 string s1("hello"); for(int i=0; i<s1.length(); i++) cout << s1.at(i) << endl; 成员函数 at 会做范围检查, 如果超出范围, 会抛出 out_of_range 异常, 而下标运算符不做范围检查 7

string 的赋值和连接 用 + 运算符连接字符串 string s1("good "), s2("morning! "); s1 += s2; cout << s1; 用成员函数 append 连接字符串 string s1("good "), s2("morning! "); s1.append(s2); cout << s1; s2.append(s1, 3, s1.size()); cout << s2; // 下标为 3 开始, s1.size() 个字符 //s1.size(), s1 字符数 // 如果字符串内没有足够字符, 则复制到字符串最后一个字符 8

比较 string 用关系运算符比较 string 的大小 ==, >, >=, <, <=,!= 返回值都是 bool 类型, 成立返回 true, 否则返回 false 例如 : string s1("hello"), s2("hello"), s3("hell"); bool b = (s1 == s2); cout << b << endl; b = (s1 == s3); cout << b << endl; b = (s1 > s3); cout << b << endl; 输出 : 1 0 1 9

比较 string 用成员函数 compare 比较 string 的大小 string s1("hello"), s2("hello"), s3("hell"); int f1 = s1.compare(s2); int f2 = s1.compare(s3); int f3 = s3.compare(s1); int f4 = s1.compare(1, 2, s3, 0, 3); //s1 1-2; s3 0-3 int f5 = s1.compare(0, s1.size(), s3); //s1 0-end cout << f1 << endl << f2 << endl << f3 << endl; cout << f4 << endl << f5 << endl; 10

比较 string 输出 0 // hello == hello 1 // hello > hell -1 // hell < hello -1 // el < hell 1 // hello > hell 11

子串 成员函数 substr() string s1("hello world"), s2; s2 = s1.substr(4,5); cout << s2 << endl; // 下标 4 开始 5 个字符 输出 : o wor 12

交换 string 成员函数 swap() string s1("hello world"), s2("really"); s1.swap(s2); cout << s1 << endl; cout << s2 << endl; 输出 : really hello world 13

string 的特性 成员函数 capacity() 返回无需增加内存即可存放的字符数成员函数 maximum_size() 返回 string 对象可存放的最大字符数成员函数 length() 和 size() 相同返回字符串的大小 / 长度成员函数 empty() 返回 string 对象是否为空 成员函数 resize() 改变 string 对象的长度 14

string 的特性 string s1("hello world"); cout << s1.capacity() << endl; cout << s1.max_size() << endl; cout << s1.size() << endl; cout << s1.length() << endl; cout << s1.empty() << endl; cout << s1 << "aaa" << endl; s1.resize(s1.length()+10); cout << s1.capacity() << endl; cout << s1.max_size() << endl; cout << s1.size() << endl; cout << s1.length() << endl; cout << s1 << "aaa" << endl; s1.resize(0); cout << s1.empty() << endl; 15

string 的特性 31 // capacity 4294967293 // maximum_size 11 // length 11 // size 0 // empty hello worldaaa // string itself and aaa 31 4294967293 21 21 hello worldaaa 1 不同编译器上可能会不一样 16

寻找 string 中的字符 成员函数 find() string s1("hello world"); s1.find("lo"); // 在 s1 中从前向后查找 lo 第一次出现的地方 // 如果找到, 返回 lo 开始的位置, 即 l 所在的位置下标 // 如果找不到, 返回 string::npos (string 中定义的静态常量 ) 成员函数 rfind() string s1("hello world"); s1.rfind("lo"); // 在 s1 中从后向前查找 lo 第一次出现的地方 // 如果找到, 返回 lo 开始的位置, 即 l 所在的位置下标 // 如果找不到, 返回 string::npos 17

寻找 string 中的字符 成员函数 find_first_of() string s1("hello world"); s1.find_first_of( abcd"); // 在 s1 中从前向后查找 abcd 中任何一个字符第一次出现的地方 // 如果找到, 返回找到字母的位置 ; 如果找不到, 返回 string::npos 成员函数 find_last_of() string s1("hello world"); s1.find_last_of( abcd"); // 在 s1 中查找 abcd 中任何一个字符最后一次出现的地方 // 如果找到, 返回找到字母的位置 ; 如果找不到, 返回 string::npos 18

寻找 string 中的字符 成员函数 find_first_not_of() string s1("hello world"); s1.find_first_not_of( abcd"); // 在 s1 中从前向后查找不在 abcd 中的字母第一次出现的地方 // 如果找到, 返回找到字母的位置 ; 如果找不到, 返回 string::npos 成员函数 find_last_not_of() string s1("hello world"); s1.find_last_not_of( abcd"); // 在 s1 中从后向前查找不在 abcd 中的字母第一次出现的地方 // 如果找到, 返回找到字母的位置 ; 如果找不到, 返回 string::npos 19

寻找 string 中的字符 string s1("hello worlld"); cout << s1.find("ll") << endl; cout << s1.find("abc") << endl; cout << s1.rfind("ll") << endl; cout << s1.rfind("abc") << endl; cout << s1.find_first_of("abcde") << endl; cout << s1.find_first_of("abc") << endl; cout << s1.find_last_of("abcde") << endl; cout << s1.find_last_of("abc") << endl; cout << s1.find_first_not_of("abcde") << endl; cout << s1.find_first_not_of("hello world") << endl; cout << s1.find_last_not_of("abcde") << endl; cout << s1.find_last_not_of("hello world") << endl; 输出 : 2 4294967295 9 4294967295 1 4294967295 11 4294967295 0 4294967295 10 4294967295 20

替换 string 中的字符 成员函数 erase() string s1("hello worlld"); s1.erase(5); cout << s1; cout << s1.length(); cout << s1.size(); // 去掉下标 5 及之后的字符 输出 : hello55 21

替换 string 中的字符 成员函数 find() string s1("hello worlld"); cout << s1.find("ll", 1) << endl; cout << s1.find("ll", 2) << endl; cout << s1.find("ll", 3) << endl; // 分别从下标 1, 2, 3 开始查找 ll 输出 : 2 2 9 22

替换 string 中的字符 成员函数 replace() string s1("hello world"); s1.replace(2,3, haha"); cout << s1; // 将 s1 中下标 2 开始的 3 个字符换成 haha 输出 : hehaha world 23

替换 string 中的字符 成员函数 replace() string s1("hello world"); s1.replace(2,3, "haha", 1,2); cout << s1; // 将 s1 中下标 2 开始的 3 个字符 // 换成 haha 中下标 1 开始的 2 个字符 输出 : heah world 24

在 string 中插入字符 成员函数 insert() string s1( hello world ); string s2( show insert ); s1.insert(5, s2); // 将 s2 插入 s1 下标 5 的位置 cout << s1 << endl; s1.insert(2, s2, 5, 3); // 将 s2 中下标 5 开始的 3 个字符插入 s1 下标 2 的位置 cout << s1 << endl; 输出 : helloshow insert world heinslloshow insert world 25

转换成 C 语言式 char * 字符串 成员函数 c_str() string s1("hello world"); printf("%s\n", s1.c_str()); // s1.c_str() 返回传统的 const char * 类型字符串 // 且该字符串以 \0 结尾 输出 : hello world 26

转换成 C 语言式 char * 字符串 成员函数 data() string s1("hello world"); const char * p1=s1.data(); for(int i=0; i<s1.length(); i++) printf("%c",*(p1+i)); //s1.data() 返回一个 char * 类型的字符串 // 对 s1 的修改可能会使 p1 出错 输出 : hello world 27

转换成 C 语言式 char * 字符串 成员函数 copy() string s1("hello world"); int len = s1.length(); char * p2 = new char[len+1]; s1.copy(p2, 5, 0); p2[5]=0; cout << p2 << endl; // s1.copy(p2, 5, 0) 从 s1 的下标 0 的字符开始, 输出 : hello // 制作一个最长 5 个字符长度的字符串副本并将其赋值给 p2 // 返回值表明实际复制字符串的长度 28