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

Similar documents
Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.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

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

第3章.doc

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

Microsoft PowerPoint - string_kruse [兼容模式]

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

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

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

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

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

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

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

Microsoft PowerPoint - 10 模板 Template.pptx

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

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

c_cpp

新版 明解C++入門編

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

C 1

C/C++ 语言 - 循环

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

untitled

Generated by Unregistered Batch DOC TO PDF Converter , please register! 浙江大学 C 程序设计及实验 试题卷 学年春季学期考试时间 : 2003 年 6 月 20 日上午 8:3

untitled

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

chap07.key

< F20B4F2D3A1D7F7D2B5>

腊八粥的来历 南宋陆游诗云 今朝佛粥更相馈 反觉江村节 物新 说的就是腊八粥 可见 腊八节 吃 腊八 粥 的风俗 由来已久 每逢腊八这一天 不论是朝 廷 官府 寺院还是黎民百姓家都要做腊八粥 这一 天 人们还要祭祀祖先 众神并庆祝丰收 后来 逐 渐演变成吃腊八粥祝来年五谷丰登 对于腊八粥的来历说法也

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

CC213

C++ 程式設計

untitled

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

Microsoft Word - CPE考生使用手冊 docx

untitled

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

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不


Microsoft Word - well_game.doc

CC213

期中考试试题讲解

書本介紹


01

FY.DOC

_汪_文前新ok[3.1].doc

提问袁小兵:

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("%

(➂)11. 炎 炎 夏 日, 即 使 下 起 滂 沱 大 雨, 都 消 除 不 了 令 人 心 煩 的 暑 氣 這 句 話 主 要 想 表 達 什 麼? ➀ 夏 日 裡 經 常 下 著 滂 沱 大 雨, 令 人 心 煩 ➁ 下 著 滂 沱 大 雨 的 日 子, 可 以 消 除 暑 氣 ➂ 夏 日

高雄市立五福國民中學九十四學年度第一學期第三次段考二年級本國語文學習領域試題卷

台北老爺校外實地參訪結案報告




Microsoft Word 養生與保健_中山大學_講義


萬里社區老人健康照護手冊

Microsoft Word - 強制汽車責任保險承保及理賠作業處理辦法 doc

Microsoft Word - 06.Understanding of Pregnancy and Birth.doc

附 件 一 : 办 理 集 中 式 银 期 转 账 业 务 网 点 名 单 序 号 地 区 网 点 名 称 地 址 联 系 人 电 话 23 工 商 银 行 安 徽 省 铜 陵 百 大 支 行 铜 陵 市 长 江 东 路 50 号 鲁 桂 珍 工 商 银 行 安 徽

2. 二 年 級 吳 毓 秀 老 師 : 感 謝 午 餐 公 司 平 時 均 能 準 時 送 餐, 但 希 望 能 不 要 使 用 加 工 品, 且 學 生 反 映 希 望 能 多 加 蛋 品 的 食 物 3. 三 年 級 柯 阿 青 老 師 : 雞 肉 有 血 水 味, 請 午 餐 公 司 能 調

人 物 春 秋 杨 永 泰 将 其 削 藩 策 略 概 括 为 : 以 经 济 方 法 瓦 解 冯 玉 祥 的 第 二 集 团 军, 以 政 治 方 法 解 决 阎 锡 山 的 第 3 集 团 军, 以 军 事 方 法 解 决 李 宗 仁 的 第 四 集 团 军, 以 外 交 方 法 对 付 张 学

,,,,,,, (,, ),,,,,,,,,,,,,,, ,,, 4 11,, ( ),,,, ( ), :, ( ),,, 1995, 66 ; ( ),, 1996, , 3-4,,

2002 4,,, 1941,,,,,,,,,,,,,,,,,, : ;:, 1991,

範本檔

糖尿病食譜


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

nooog

OOP with Java 通知 Project 2 提交时间 : 3 月 21 日晚 9 点 作业提交格式 学习使用 文本编辑器 cmd, PowerShell (Windows), terminal(linux, Mac)

C/C++语言 - 分支结构

untitled

幻灯片 1

ebook39-5

Guava学习之Resources

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

立 志 于 打 造 最 贴 近 考 生 实 际 的 辅 导 书 计 算 机 考 研 之 数 据 结 构 高 分 笔 记 率 辉 编 著 周 伟 张 浩 审 核 讨 论 群 :

2015年计算机二级(C语言)模拟试题及答案(三)

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

PowerPoint 演示文稿

ebook8-30

download.kaoyan.com_2006ÄêÌì½ò¹¤Òµ´óѧ¸ß¼¶ÓïÑÔ³ÌÐòÉè¼Æ£¨409£©¿¼ÑÐÊÔÌâ

Department of Science and Engineering Computing School of Mathematics School Peking University October 9, 2007

Chapter12 Derived Classes

PowerPoint 演示文稿

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

Microsoft Word - 第3章.doc

Microsoft Word - chap13.doc

华恒家庭网关方案

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

CC213

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

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

epub 33-8

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

校园之星

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


Transcription:

Arrays and Strings 存储同类型的多个元素 Store multi elements of the same type

数组 (array) 存储固定数目的同类型元素 如整型数组存储的是一组整数, 字符数组存储的是一组字符 数组的大小称为数组的尺度 (dimension). 定义格式 : type arrayname[dimension]; 如声明 4 个元素的整型数组 :intarr[4]; 可用索引 (index) 访问数组的元素, 索引 ( 也称为下标 ) 从 0 开始编号, 如访问 arr 的第三个元素可通过 arr[2] arr[2] = 20;

数组 (array)- 应初始化 否则会导致不可预期的逻辑错误 初始化有多种方式 : - 初始化方式 1: 先声明数组, 然后初始化其中的元 素 如 : int arr[4]; arr[0] = 6; arr[1] = 0; arr[2] = 9; arr[3] = 6; 6 0 9 6

数组 (array)- 应初始化 - 初始化方式 2: 声明时初始化其中的部分或全部元素 如 : int arr[4] = { 6, 0, 9, 6 ; 这种方式初始化全部元素时, 有时省略数组个数的说明, 让编译器帮我们推断数组的大小 如 : int arr[] = { 6, 0, 9, 6, 2, 0, 1, 1 ; 访问数组元素时, 其索引是 0 到 n-1 的整数 (n 是数组的大小 ) 或整型表达式, 否则会引起程序崩溃 int arr[] = { 6, 0, 9, 6, 2, 0, 1, 1 ; int i =2; arr[5]; arr[i]; arr[i+3];

数组 (array)- 应初始化

数组 (array)- 作为函数参数 数组可以作为一个参数传给函数 声明函数时, 只要指明该参数是一个 ( 但不带大小 ) 数组 如

数组 (array)- 作为函数参数 数组是作为引用 (reference) 被传递给函数的, 因此在函数内对数组的修改也是对实际参数的修改 void f(int arr[]){ arr[2] = 33; int main(){ int a[] = {1,2,3,4,5; f(a); for(int i = 0 ; i<5 ;i++) std::cout<< a[i]<< ; return 0;

多维数组 (multidimensional array) 如二维数组 : type arrayname[dimension1][dimension2]; 数组将包含 dimension1*dimension2 个元素. 第一索引将指示 dimension1 个子数组, 第二索引将指示 dimension2 个子数组 初始化和访问方式类似于一维数组

多维数组 (multidimensional array) 6 0 9 6 2 0 1 1

多维数组 (multidimensional array) 也可如下方式初始化数组 : int twodimarray[2][4] = { 6, 0, 9, 6, 2, 0, 1, 1 ; int twodimarray[2][4] = { { 6, 0, 9, 6, { 2, 0, 1, 1 ; 声明多维数组必须指定每一维的大小, 作为函数参数时, 除第一维外, 必须指定其他维的大小 int afunction(int arr[][4]) { 多维数组仅仅是程序员的抽象, 实际上在内存内仍然是和一维数组一样 : 是存放在内存的一个序列 因此 int arr[2][4]; 和 在内存中是同一个东西! int arr[8];

字符串 (Strings) 字符串实际就是一串连续的字符序列, 所以我们也可以用简单的字符数组来表示它 例如 :char jenny [16]; 表示一个最多可以存储 16 个字符的数组 它也可以存储比这短的字符序列, 而且实际中常常如此 一种习惯在字符串的有效内容的结尾处加一个空字符 (null character) 来表示字符结束, 它的常量表示可写为 0 或 '\0' H e l l o \0 H e l l o, w o r l d! \0

字符串 (Strings) C 语言字符串 : 以空字符 \0 结束的字符数组 两种初始化方式 : char mystring[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' ; char mystring[ ] = "Hello";

字符串 (Strings) 字符串 : 以空字符 \0 结束的字符数组 #Include <stdio.h> int main(){ char str[10]; str[0] = H ; str[1] = e ; str[2] = l ; str[3] = l ; str[4] = o ; printf( str = %s,str); str[5] = \0 ; printf( str = %s,str); H e l l o \0 字符串可以用文字常量初始化 : int helloworld[] = Hello, world! ; 编译器会自动在最后插入空字符 字符数组字符串

C/C++ 标准库中的字符串处理函数 C 标准库提供了不同的字符串处理函数, 这些函数的原型在下列文件中说明了 C++ 标准库提供了 cstring 库取代 string.h

#include <iostream> #include <cctype> using namespace std; intmain() { char str[ ] = "t6h0i9s6.is.999a9.string"; charch= str[0]; for(inti= 0; ch!= '\0'; ch = str[++i]) { if(isalpha(ch)) cout << (char)(isupper(ch)? tolower(ch) : ch); else if(ispunct(ch)) cout<< ' '; cout << endl; return 0;

用 cstring 取代 string.h

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ int strlen(char str[]){ int i = 0 ; while (str[i]!= \0 ) i++; return i; /* 字符串拼接 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ;

cstring/string.h 的函数实现模拟 /* 字符串拼接 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=0 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=1 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=2 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=3 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=4 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=5 w o r l d \0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o \0 i=5 w o r l d \0 j=0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o w i=5 w o r l d \0 j=0

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o w o w o r l d \0 j=1 i=6

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o w o r w o r l d \0 j=2 i=7

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l w o r l d \0 l o w o r l j=3 i=8

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l w o r l d \0 l o w o r l d j=4 i=9

cstring/string.h 的函数实现模拟 /* 求字符串长度 */ void strcat(char str[],char str2[]){ int i = 0 ; while (str[i]!= \0 ) i++; for(int j = 0 ; str2[j]!= \0 ;j++,i++) str[i] = str2[j]; str[i] = \0; return ; h e l l o w o r l d \0 w o r l d \0 j=5 i=10