摘要:
// 15 char型字符串与函数/*#include <iostream>using namespace std;//int get(const char*p);int get(const char p[]);int main(){ //char型字符串也就是C风格的字会串,它由一个串字符组成,结尾为字符串结束标志'\0' //字符串名是第一个字符的地址,因此我们将字符串名作为参数传递到函数时,其实就是将地址传递到函数中去 //由于字符串的最后一个字符是'\0',因此我们不必传递字符长度,只要在函数中设置一个循环体,把结束字符作为循环结束的条件即可 阅读全文
摘要:
// 16 函数如何返回字符串/*#include <iostream>using namespace std;char *get(char *str);int main(){ //上一节的末尾处讲过,只要获得字符串中第一个字符的地址就可依次找到其它字符,因此只要将字符串中第一个字符的地址返回,就相当于返回了整个字符串 //既然它们都可看作是字符串第一个字符的地址,那么在函数中直接将它们返回,也就等同于返回了字符串的第一个字符的地址,从而间接地达到了返回整个字符串的目前的,另外由于返回的是地址,自然而然地避免了调用复制构造函数,系统的开销也大大减少 char c[10] = &qu 阅读全文
摘要:
// 12判断string类型字符串是否为空/*#include <iostream>#include <string>using namespace std;int main(){ string s1 = ""; if(s1.empty()){ cout<<"s1为空"<<endl; }else{ cout<<"s1不为空:"<<s1<<endl; } return 0; */ 阅读全文
摘要:
// 13 交换两个字符串内容的swab函数/*#include <iostream>#include <string>using namespace std;int main(){ //C库函数中有一个swab函数,该函数用来交换字节,那么可不可以交换两个字符串的内容呢? char ch1[15] = "ofru"; char ch2[] = ""; //swab(ch1,ch2,strlen(ch1)); //不知道什么毛病,用swab总是动行后提示错误 //cout<<"ch1:"<&l 阅读全文
摘要:
// 14 将string型字符串转为char字符串/*#include <iostream>#include <string>using namespace std;int main(){ string str="hello word"; const char *ch; ch = str.c_str(); cout<<ch<<endl; return 0;}*/ 阅读全文