上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 49 下一页
摘要: // 15 char型字符串与函数/*#include <iostream>using namespace std;//int get(const char*p);int get(const char p[]);int main(){ //char型字符串也就是C风格的字会串,它由一个串字符组成,结尾为字符串结束标志'\0' //字符串名是第一个字符的地址,因此我们将字符串名作为参数传递到函数时,其实就是将地址传递到函数中去 //由于字符串的最后一个字符是'\0',因此我们不必传递字符长度,只要在函数中设置一个循环体,把结束字符作为循环结束的条件即可 阅读全文
posted @ 2012-09-24 22:22 简单--生活 阅读(597) 评论(0) 推荐(0) 编辑
摘要: // 16 函数如何返回字符串/*#include <iostream>using namespace std;char *get(char *str);int main(){ //上一节的末尾处讲过,只要获得字符串中第一个字符的地址就可依次找到其它字符,因此只要将字符串中第一个字符的地址返回,就相当于返回了整个字符串 //既然它们都可看作是字符串第一个字符的地址,那么在函数中直接将它们返回,也就等同于返回了字符串的第一个字符的地址,从而间接地达到了返回整个字符串的目前的,另外由于返回的是地址,自然而然地避免了调用复制构造函数,系统的开销也大大减少 char c[10] = &qu 阅读全文
posted @ 2012-09-24 22:22 简单--生活 阅读(328) 评论(0) 推荐(0) 编辑
摘要: // 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; */ 阅读全文
posted @ 2012-09-24 22:07 简单--生活 阅读(663) 评论(0) 推荐(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 阅读全文
posted @ 2012-09-24 22:07 简单--生活 阅读(2041) 评论(0) 推荐(0) 编辑
摘要: // 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;}*/ 阅读全文
posted @ 2012-09-24 22:07 简单--生活 阅读(215) 评论(0) 推荐(0) 编辑
摘要: // 11 string字符串的比较#include <iostream>#include <string>using namespace std;int main(){ string s1 = "155"; string s2 = "52"; char c[] = "34"; int i, j, k, l, m, n; i = s1.compare(s2); //compare比较的事字符而不是数字 //compare 将s1与s2比较,返回0为相等,1为s1大于s2,-1为s1小于s2; cout<& 阅读全文
posted @ 2012-09-23 15:48 简单--生活 阅读(358) 评论(0) 推荐(0) 编辑
摘要: // 9 string类的erase成员函数的使用/*#include <iostream>#include <string>using namespace std;int main(){ string s = "give me"; cout<<"原始字符串为:"<<s<<endl; s.erase(1,3); //炎1开始删除三个字符 cout<<"原始字符串为:"<<s<<endl; s.erase(2); //删除二个字符,从后面开始 阅读全文
posted @ 2012-09-23 15:47 简单--生活 阅读(257) 评论(0) 推荐(0) 编辑
摘要: // 10string 型字符串的查找/*#include <iostream>using namespace std;int main(){ char ch1[20]; char *p, c='w'; strcpy(ch1,"hellwo wordlxxdxxd"); p = strchr(ch1,c); //在字符串中查找字符 if(p){ //这里的p-ch1, 相当于p在ch1中的位置 //这里的p是指的从查找到字符后面的所有字符, //这里的位置有点不清楚样 //还要提高 //由于一个字符占一个字节,因此用p-ch1就是用求出p相对于c 阅读全文
posted @ 2012-09-23 15:47 简单--生活 阅读(694) 评论(0) 推荐(0) 编辑
摘要: // 7 string型字符串的拷贝/*#include <iostream>#include <string>using namespace std;int main(){ //char ch1[15] = "hello word"; //char ch2[] = "xiang ling chuan"; //cout<<"源字符串"<<ch1<<endl; //memmove(ch1,ch2,5); //cout<<"拷贝后:"<< 阅读全文
posted @ 2012-09-23 15:44 简单--生活 阅读(451) 评论(0) 推荐(0) 编辑
摘要: //8 string类insert成员函数的使用/*#include <iostream>#include <string>using namespace std;int main(){ string str1 ="php china"; string str2 = "hello word"; str1.insert(4,str2,0,5); cout<<str1<<endl; return 0;}*/ 阅读全文
posted @ 2012-09-23 15:44 简单--生活 阅读(822) 评论(0) 推荐(0) 编辑
上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 49 下一页
简单--生活(CSDN)