字符数组和 string类型比较的区别

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class area{
  6. public:
  7. area(){ cout << "gouzao:" <<endl; }
  8. area(int i){ this->i = i; cout << "gouzao:" << i << endl; }
  9. area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; }
  10. ~area(){ cout << "xigou "<<i << endl; }
  11. int get()
  12. {
  13. return w*h;
  14. }
  15. void set(int w, int h)
  16. {
  17. this->w = w;
  18. this->h = h;
  19. }
  20. private:
  21. int w;
  22. int h;
  23. int i;
  24. };
  25.  
  26. int main()
  27. {
  28.  
  29. //char 数组做比较需要 strcmp
  30. //char a[] = "ab";
  31. //char b[] = "ab";
  32. //cout << (a==b) << endl;//不相等,比较的是两个地址
  33.  
  34. //cout << strcmp(a, b) << endl;
  35. //cout << strcmp(a, "ab") << endl;
  36.  
  37. string a = "ab";
  38.  
  39. cout << (a == "ab") << endl; //wright
  40.  
  41. return 0;
  42.  
  43. }

赋值比较:

  1. int main()
  2. {
  3.  
  4. char ch1[10] = "ab";
  5. char ch2[10] = "cd";
  6.  
  7. //ch1 = ch2; //错误
  8. strcpy(ch1, ch2);
  9.  
  10. string ch3 = "ab";
  11. string ch4 = "cd";
  12.  
  13. ch3 = ch4;
  14.  
  15. cout << ch3 << endl;
  16. cout << ch4 << endl;
  17.  
  18. return 0;
  19.  
  20. }

字符串赋值:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class area{
  6. public:
  7. area(){ cout << "gouzao:" <<endl; }
  8. area(int i){ this->i = i; cout << "gouzao:" << i << endl; }
  9. area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; }
  10. ~area(){ cout << "xigou "<<i << endl; }
  11. int get()
  12. {
  13. return w*h;
  14. }
  15. void set(int w, int h)
  16. {
  17. this->w = w;
  18. this->h = h;
  19. }
  20. private:
  21. int w;
  22. int h;
  23. int i;
  24. };
  25.  
  26. int main()
  27. {
  28.  
  29. //char ch1[10] = "ab";
  30. //char ch2[10] = "cd";
  31.  
  32. ////ch1 = ch2; //错误
  33. //strcpy(ch1, ch2);
  34.  
  35. string ch3 = "ab";
  36. string ch4 = "cd";
  37.  
  38. ch3 = ch4;
  39.  
  40. ch3.assign(ch4,,); //部分元素赋值给ch3
  41.  
  42. cout << ch3 << endl;
  43. cout << ch4 << endl;
  44.  
  45. return ;
  46.  
  47. }

字符串合并:

  1. int main()
  2. {
  3.  
  4. string ch1 = "ab";
  5. string ch2 = "cd";
  6.  
  7. ch1 = ch1 + ch2;
  8.  
  9. cout << ch1 << endl;
  10. cout << ch2 << endl;
  11.  
  12. return ;
  13.  
  14. }

计算长度:

  1. int main()
  2. {
  3.  
  4. string ch1 = "ab";
  5. string ch2 = "cd";
  6.  
  7. ch1 = ch1 + ch2;
  8.  
  9. cout << ch1 << endl;
  10. cout << ch2 << endl;
  11.  
  12. //计算字符长度
  13. cout << strlen(ch1.c_str()) << endl;
  14. cout << ch1.size() << endl;
  15. cout << ch1.length() << endl;
  16.  
  17. return ;
  18.  
  19. }

字符串部分合并:

  1. int main()
  2. {
  3.  
  4. //char ch3[10] = "ab";
  5. //char ch4[10] = "abcdefg";
  6.  
  7. //strncat(ch3, ch4, 3);
  8.  
  9. //cout << ch3 << endl;
  10.  
  11. string ch1 = "ab";
  12. string ch2 = "cdefg";
  13.  
  14. ch1.append(ch2, ,);
  15. cout << ch1 << endl;
  16.  
  17. return ;
  18.  
  19. }

字符串替换:

  1. int main()
  2. {
  3.  
  4. //char ch3[10] = "ab";
  5. //char ch4[10] = "abcdefg";
  6.  
  7. //strncpy(ch3, ch4, 3);
  8.  
  9. //cout << ch3 << endl;
  10.  
  11. string ch1 = "ab";
  12. string ch2 = "cdefg";
  13. char ch5[] = "cdefg";
  14. char ch6 = 'A';
  15.  
  16. //ch1.replace(0,1,ch2, 2,2);
  17. //ch1.replace(0, 1, ch5, 2, 2);//支持char型数组
  18. //ch1.replace(0, 1, 2, ch6);//支持char 字符
  19.  
  20. cout << ch1 << endl;
  21.  
  22. return ;
  23.  
  24. }

字符串拷贝:

  1. int main()
  2. {
  3.  
  4. char ch3[] = "abffffff";
  5. char ch4[] = "abcdefg";
  6.  
  7. memmove(ch3, ch4, );
  8.  
  9. cout << ch3 << endl;
  10.  
  11. string ch1 = "abcdefghkjklmn";
  12. char ch5[] = "cdefg";
  13.  
  14. cout << ch5 << endl;
  15.  
  16. ch1.copy(ch5,,);
  17.  
  18. cout << ch5 << endl;
  19.  
  20. return ;
  21.  
  22. }

字符串插入:

  1. int main()
  2. {
  3.  
  4. string str1 = "abcdefg";
  5. string str2 = "abc";
  6.  
  7. str1.insert(,str2, ,);
  8.  
  9. cout << str1 << endl;
  10.  
  11. return ;
  12.  
  13. }

字符串删除:

  1. int main()
  2. {
  3.  
  4. string str1 = "abcdefg";
  5. str1.erase(,);
  6.  
  7. cout << str1 << endl;
  8.  
  9. return ;
  10.  
  11. }

删除字符串:

  1. int main()
  2. {
  3.  
  4. string str1 = "abcdefg";
  5. str1.erase(,);
  6. cout << str1 << endl;
  7.  
  8. str1.erase();//第二个以后全部删除
  9. cout << str1 << endl;
  10.  
  11. str1.erase();//清空一个字符串
  12. cout << str1 << endl;
  13.  
  14. return ;
  15.  
  16. }

字符串查找:

  1. int main()
  2. {
  3. char ch1[] = "hello world!";
  4. char *p, c = '';
  5.  
  6. p = strchr(ch1, c);//返回找到的w字符的地址,找不到则返回空指针
  7.  
  8. if (p)
  9. {
  10. cout << p << endl;
  11. cout << p - ch1 << endl; //计算找到的字符的下标
  12. }
  13.  
  14. return ;
  15.  
  16. }
  1. int main()
  2. {
  3. string str1("abcdefg");
  4.  
  5. int f = str1.find('b', ); //从第一个字符开始查找
  6.  
  7. if (f == string::npos)
  8. {
  9. cout << "not find " << endl;
  10. }
  11. cout << f << endl;
  12.  
  13. f = str1.find_first_not_of('b', ); //查找第一个不是b的字符
  14. cout << f << endl;
  15.  
  16. f = str1.find_last_of('b'); //查找最后一个b的位置
  17.  
  18. cout << (int)string::npos << endl;
  19.  
  20. return ;
  21.  
  22. }

判断字符串是否为空:

  1. int main()
  2. {
  3.  
  4. string s1 = "";
  5.  
  6. if (s1.empty())
  7. {
  8. cout << "empty" << endl;
  9. }else{
  10. cout << "not empty" << endl;
  11. }
  12. return ;
  13.  
  14. }

字符串交换:

  1. int main()
  2. {
  3. char ch1[] = "ofru";
  4. char ch2[] = "";
  5. swap(ch1,ch2);
  6.  
  7. cout << ch1 << endl;
  8. cout << ch2 << endl;
  9.  
  10. string str1 = "ab";
  11. string str2 = "";
  12. str1.swap(str2);
  13.  
  14. cout << str1 << endl;
  15. cout << str2 << endl;
  16.  
  17. return ;
  18.  
  19. }

string字符串转char型

  1. int main()
  2. {
  3. string str1 = "abcde";
  4.  
  5. const char *p;
  6. p = str1.c_str();
  7.  
  8. cout << p << endl;
  9.  
  10. return ;
  11.  
  12. }

字符串传参:

  1. int get_length(const char *p)
  2. //int get_length(const char p[])
  3. {
  4. int count = ;
  5. while (*p)
  6. {
  7. count++;
  8. p++;
  9. }
  10.  
  11. return count;
  12. }
  13.  
  14. int main()
  15. {
  16.  
  17. char a[] = "abc";
  18. char *p = "defg";
  19.  
  20. cout << get_length(a) << endl;
  21. cout << get_length(p) << endl;
  22.  
  23. return ;
  24.  
  25. }

字符串函数返回:

  1. char *get(const char *str)
  2. {
  3. char *p = new char[strlen(p) +];
  4. strcpy(p, str);
  5.  
  6. return p;
  7. }
  8.  
  9. int main()
  10. {
  11.  
  12. char a[] = "abc";
  13. char *p = get(a);
  14.  
  15. cout << p << endl;
  16.  
  17. char *p2 = get("abc");
  18. cout << p << endl;
  19.  
  20. char *p3 = "abc";
  21. char *p4 = get(p3);
  22. cout << p4 << endl;
  23.  
  24. delete[]p;
  25.  
  26. return ;
  27.  
  28. }

结构体:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct man{
  6. public:
  7. int age;
  8. char *name;
  9.  
  10. };
  11.  
  12. int main()
  13. {
  14. struct man one={
  15. ,
  16. "中国"
  17. };
  18.  
  19. cout << one.age << endl;
  20. cout <<one.name<< endl;
  21.  
  22. return ;
  23.  
  24. }

结构体与构造函数:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct man{
  6. man(int c_age);
  7. //public:
  8. int age;
  9. char *name;
  10. string name1;
  11.  
  12. };
  13.  
  14. man::man(int c_age)
  15. {
  16. age = c_age;
  17. }
  18.  
  19. int main()
  20. {
  21. man one();
  22.  
  23. cout << one.age << endl;
  24.  
  25. return ;
  26. }

结构体赋值:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct man{
  6. int age;
  7. char *name;
  8. string name1;
  9.  
  10. };
  11.  
  12. int main()
  13. {
  14. man one = {
  15. , "one", "one1"
  16. };
  17.  
  18. man two = {
  19. , "two", "two1"
  20. };
  21.  
  22. one = two;
  23.  
  24. cout << one.age << endl;
  25. cout << one.name << endl;
  26. cout << one.name1 << endl;
  27.  
  28. return ;
  29. }

结构体与函数:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct time{
  6. int hour;
  7. int minute;
  8. };
  9.  
  10. const int perhour = ;
  11. time sum(time t1, time t2);
  12.  
  13. int main()
  14. {
  15.  
  16. time t1 = {,};
  17. time t2 = { , };
  18.  
  19. time total = sum(t1,t2);
  20.  
  21. cout << total.hour << endl;
  22. cout << total.minute << endl;
  23.  
  24. return ;
  25. }
  26.  
  27. time sum(time t1, time t2)
  28. {
  29. time total;
  30. total.minute = (t1.minute + t2.minute) % perhour;
  31. total.hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour);
  32.  
  33. return total;
  34.  
  35. }

指针方式返回:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct time{
  6. int hour;
  7. int minute;
  8. };
  9.  
  10. const int perhour = ;
  11. time *sum(const time &t1, const time &t2);
  12.  
  13. int main()
  14. {
  15. time t1 = {,};
  16. time t2 = { , };
  17.  
  18. time *total = sum(t1,t2);
  19.  
  20. cout << total->hour << endl;
  21. cout << total->minute << endl;
  22.  
  23. return ;
  24. }
  25.  
  26. time *sum(const time &t1, const time &t2)
  27. {
  28. time *total = new time;
  29. total->minute = (t1.minute + t2.minute) % perhour;
  30. total->hour = t1.hour + t2.hour + ((t1.minute + t2.minute) / perhour);

  31.   delete total;
  32. return total;
  33.  
  34. }

引用方式传递返回:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. const string &show(const string &str);
  6.  
  7. int main()
  8. {
  9.  
  10. string str1 = "abc";
  11. string str2 = show(str1);
  12.  
  13. cout << str2 << endl;
  14.  
  15. return ;
  16. }
  17.  
  18. const string &show(const string &str)
  19. {
  20. return str;
  21. }

c++学习-字符串的更多相关文章

  1. .Net程序员之Python基础教程学习----字符串的使用 [Second Day]

         在The FirstDay 里面学习了列表的元组的使用,今天开始学习字符串的使用.字符串的使用主要要掌握,字符串的格式化(C语言中我们应该都知道,Python和C语言差别不大),字符串的基本 ...

  2. 03- Shell脚本学习--字符串和数组

    字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似: 单双引号的区别: 双 ...

  3. C语言学习 —— 字符串的学习(一)

    这是本人在学习 C语言有关 字符串内容 时的相关笔记 由于本人技术有限,如有错误,还望指正 C语言中数据类型中只有 字符型(char),而 char型 变量一次只能存储一个字符,在日常工作中经常需要定 ...

  4. Day2 Python基础学习——字符串、列表、元组、字典、集合

    Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...

  5. python学习--字符串

    python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...

  6. Swift学习—字符串&数组&字典

    字符串 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符串"" ...

  7. Java学习----字符串函数

    1.String string类是最终类,不可以有子类 package com.demo.pkg1; public class TestString { public static void main ...

  8. python学习-字符串前面添加u,r,b的含义

    引用:https://www.cnblogs.com/cq90/p/6959567.html u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unico ...

  9. Swift学习字符串、数组、字典

    一.字符串的使用 let wiseWords = "\"I am a handsome\"-boy" var emptyString = "" ...

随机推荐

  1. Oracle学习系列1

    两个服务必须启动: OracleOraDb10g*TNListener 和 OracleService*** 使用sqlplusw先进行环境的设置 set linesize 300 ; set pag ...

  2. C++ Unicode SBCS 函数对照表

    C++ Unicode SBCS 函数对照表,以备日后查阅 Generic SBCS UNICODE TCHAR char wchar_t _TEOF EOF WEOF _TINT int wint_ ...

  3. Fortran编译多个文件(转载)

    最近需要在Linux系统下编译多个Fortran程序,在网上搜索了一下,但是资料不多,也许因为这个问题比较简单,不值一提,但还是把我知道的写出来,供大家参考: 方法一: 假如现在有两个Fortran程 ...

  4. Comparison of B-Tree and Hash Indexes

    Understanding the B-tree and hash data structures can help predict how different queries perform on ...

  5. ABBYY将JPEG文件转换成Word文档的方法

    日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...

  6. sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数

    https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...

  7. linux服务之rsync

    http://www.cnblogs.com/itech/archive/2010/06/13/1757952.html rsync与mfs好像有点类似,都是传输块的chunk,chunk的 1)软件 ...

  8. Swagger-UI 基于REST的API测试/文档类插件

    现在多数的项目开发中,网站和移动端都需要进行数据交互和对接,这少不了使用REST编写API接口这种场景.例如我目前的工作,移动端交由了另一团队开发,不同开发小组之间就需要以规范和文档作为标准和协作基础 ...

  9. win10 phpStudy 80端口被占用

    原因是win8下系统默认占用80端口,导致apache无法打开.解决方法: 1.以管理员权限运行c:windowssystem32cmd.exe :2.C:WINDOWSsystem32>net ...

  10. python sys.argv[]

    sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...