沈阳世纪兴网站建设,毕业设计心理评测网站开发,wordpress框架分析,网站做任务给钱的目录 1. 字符与整数的联系——ASCII码每个常用字符都对应一个-128 ~ 127的数字#xff0c;二者之间可以相互转化。注意#xff1a;目前负数没有与之对应的字符。
2.字符数组
2.2 字符数组的常用操作下面几个函数需要引入头文件:
2.3 遍历字符数组中的字符#xff1a;
3.…目录 1. 字符与整数的联系——ASCII码每个常用字符都对应一个-128 ~ 127的数字二者之间可以相互转化。注意目前负数没有与之对应的字符。
2.字符数组
2.2 字符数组的常用操作下面几个函数需要引入头文件:
2.3 遍历字符数组中的字符
3. 标准库类型string可变长的字符序列比字符数组更加好用。需要引入头文件
3.1 定义和初始化
3.2 string上的操作
(2) 使用getline读取一整行
(3) string的empty和size操作注意size是无符号整数因此 s.size() -1一定成立
(4) string的比较
(5) 为string对象赋值
(6) 两个string对象相加
(7) 字面值和string对象相加
3.3 处理string对象中的字符可以将string对象当成字符数组来处理
4.string中元素的访问
1、[ ]下标 因为string类对[ ]运算符进行了重载所以我们可以直接使用[ ]下标访问对象中的元素。并且该重载使用的是引用返回所以我们可以通过[ ]下标修改对应位置的元素。
2、使用at访问对象中的元素 因为at函数也是使用的引用返回所以我们也可以通过at函数修改对应位置的元素。
3、使用范围for访问对象中的元素
5.string中子字符串的提取
1、使用substr函数提取string中的子字符串 1. 字符与整数的联系——ASCII码 每个常用字符都对应一个-128 ~ 127的数字二者之间可以相互转化。注意目前负数没有与之对应的字符。
#include iostreamusing namespace std;int main()
{char c a;cout (int)c endl;int a 66;cout (char)a endl;return 0;
}
常用ASCII值A- Z是65 ~ 90a - z是97 - 1220 - 9是 48 - 57。 字符可以参与运算运算时会将其当做整数
#include iostreamusing namespace std;int main()
{int a B - A;int b A * B;char c A 2;cout a endl;cout b endl;cout c endl;return 0;
}
2.字符数组
字符串就是字符数组加上结束符\0。
可以使用字符串来初始化字符数组但此时要注意每个字符串结尾会暗含一个\0字符因此字符数组的长度至少要比字符串的长度多 1 #include iostreamusing namespace std;int main()
{char str[100];cin str; // 输入字符串时遇到空格或者回车就会停止cout str endl; // 输出字符串时遇到空格或者回车不会停止遇到\0时停止printf(%s\n, str);return 0;
}
读入一行字符串包括空格
#include iostreamusing namespace std;int main()
{char str[100];fgets(str, 100, stdin); // gets函数在新版C中被移除了因为不安全。// 可以用fgets代替但注意fgets不会删除行末的回车字符cout str endl;return 0;
}
2.2 字符数组的常用操作 下面几个函数需要引入头文件:
#include string.h (1) strlen(str)求字符串的长度 (2) strcmp(a, b)比较两个字符串的大小a b返回-1a b返回0a b返回1。这里的比较方式是字典序 (3) strcpy(a, b)将字符串b复制给从a开始的字符数组。
#include iostream
#include string.husing namespace std;int main()
{char a[100] hello world!, b[100];cout strlen(a) endl;strcpy(b, a);cout strcmp(a, b) endl;return 0;
}
2.3 遍历字符数组中的字符
#include iostream
#include string.husing namespace std;int main()
{char a[100] hello world!;// 注意下述for循环每次均会执行strlen(a)运行效率较低最好将strlen(a)用一个变量存下来for (int i 0; i strlen(a); i )cout a[i] endl;return 0;
}
3. 标准库类型string 可变长的字符序列比字符数组更加好用。需要引入头文件
#include string
3.1 定义和初始化
#include iostream
#include stringusing namespace std;int main()
{string s1; // 默认初始化s1是一个空字符串string s2 s1; // s2是s1的副本注意s2只是与s1的值相同并不指向同一段地址string s3 hiya; // s3是该字符串字面值的副本string s4(10, c); // s4的内容是 ccccccccccreturn 0;
}
3.2 string上的操作
(1) string的读写
#include iostream
#include stringusing namespace std;int main()
{string s1, s2;cin s1 s2;cout s1 s2 endl;return 0;
}
注意不能用printf直接输出string需要写成printf(“%s”, s.c_str());
(2) 使用getline读取一整行
#include iostream
#include stringusing namespace std;int main()
{string s;getline(cin, s);cout s endl;return 0;
}
(3) string的empty和size操作注意size是无符号整数因此 s.size() -1一定成立
#include iostream
#include stringusing namespace std;int main()
{string s1, s2 abc;cout s1.empty() endl;cout s2.empty() endl;cout s2.size() endl;return 0;
}
(4) string的比较
支持 , , , , , !等所有比较操作按字典序进行比较。
(5) 为string对象赋值
string s1(10, c), s2; // s1的内容是 ccccccccccs2是一个空字符串
s1 s2; // 赋值用s2的副本替换s1的副本// 此时s1和s2都是空字符串
(6) 两个string对象相加
做加法运算时字面值和字符都会被转化成string对象因此直接相加就是将这些字面值串联起来string s1 hello, s2 world; // 在s1和s2中都没有标点符号
string s3 s1 , s2 \n;
(7) 字面值和string对象相加
做加法运算时字面值和字符都会被转化成string对象因此直接相加就是将这些字面值串联起来
string s1 hello, s2 world; // 在s1和s2中都没有标点符号
string s3 s1 , s2 \n;
当把string对象和字符字面值及字符串字面值混在一条语句中使用时必须确保每个加法运算符的两侧的运算对象至少有一个是string
string s4 s1 , ; // 正确把一个string对象和有一个字面值相加
string s5 hello , ; // 错误两个运算对象都不是stringstring s6 s1 , world; // 正确每个加法运算都有一个运算符是string
string s7 hello , s2; // 错误不能把字面值直接相加运算是从左到右进行的
3.3 处理string对象中的字符 可以将string对象当成字符数组来处理
#include iostream
#include stringusing namespace std;int main()
{string s hello world;for (int i 0; i s.size(); i )cout s[i] endl;return 0;
}
或者使用基于范围的for语句
#include iostream
#include stringusing namespace std;int main()
{string s hello world;for (char c: s) cout c endl;for (char c: s) c a;cout s endl;return 0;
}
4.string中元素的访问
1、[ ]下标 因为string类对[ ]运算符进行了重载所以我们可以直接使用[ ]下标访问对象中的元素。并且该重载使用的是引用返回所以我们可以通过[ ]下标修改对应位置的元素。
#include iostream
#include string
using namespace std;
int main()
{string s(CSDN);//[]下标访问对象元素for (size_t i 0; i s.size(); i){cout s[i];}cout endl;//[]下标修改对象元素内容for (size_t i 0; i s.size(); i){s[i] x;}cout s endl; //xxxxreturn 0;
}2、使用at访问对象中的元素 因为at函数也是使用的引用返回所以我们也可以通过at函数修改对应位置的元素。
#include iostream
#include string
using namespace std;
int main()
{string s(CSDN);for (size_t i 0; i s.size(); i){//at(pos)访问pos位置的元素cout s.at(i);}cout endl;for (size_t i 0; i s.size(); i){//at(pos)访问pos位置的元素并对其进行修改s.at(i) x;}cout s endl; //xxxxreturn 0;
}3、使用范围for访问对象中的元素
需要特别注意的是若是需要通过范围for修改对象的元素则用于接收元素的变量e的类型必须是引用类型否则e只是对象元素的拷贝对e的修改不会影响到对象的元素。
#include iostream
#include string
using namespace std;
int main()
{string s(CSDN);//使用范围for访问对象元素for (auto e : s){cout e;}cout endl; //CSDN//使用范围for访问对象元素并对其进行修改for (auto e : s) //需要修改对象的元素e必须是引用类型{e x;}cout s endl; //xxxxreturn 0;
}5.string中子字符串的提取
1、使用substr函数提取string中的子字符串
#include iostream
#include string
using namespace std;
int main()
{string s1(abcdef);string s2;//substr(pos, n)提取pos位置开始的n个字符序列作为返回值s2 s1.substr(2, 4);cout s2 endl; //cdefreturn 0;
}#include iostream
#include string
using namespace std;
int main()
{string s(abcdef);char str[20];//copy(str, n, pos)复制pos位置开始的n个字符到str字符串size_t length s.copy(str, 4, 2);//copy函数不会在复制内容的末尾附加\0需要手动加str[length] \0;cout str endl; //cdefreturn 0;
}