标准库中的字符串类

  C++语言直接支持C语言所有概念。

  C++中没有原生的字符串类型。

由于C++中没有原生的字符串类型,C++标准库提供了string类型。

  1、string 直接支持字符串链接

  2、字符串大小比较

    /*实验 字符串排序 拼接*/    

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6. /*实验1 字符串排序 拼接*/
  7. string string_add(string s[],int addnumber)
  8. {
  9. string ret="";
  10. for(int i= ; i<addnumber ;i++)
  11. {
  12. ret += s[i]+';';//字符串拼接
  13. }
  14. return ret;
  15. }
  16.  
  17. //首字母排序 swap();C++交换函数
  18. //按照首字母排序
  19. void string_sord(string s[],int len)
  20. {
  21. for(int i = ;i<len ; i++)
  22. {
  23. for(int j=i ;j<len;j++)
  24. if(s[i] > s[j])
  25. swap(s[i],s[j]);//使用 swap()交换两个字符串
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. string sTest[] =
  32. {
  33. "abc",
  34. "cba",
  35. "bwe"
  36. };
  37. cout<<string_add(sTest,)<<endl;
  38. cout<<endl;
  39. string_sord(sTest,);
  40. for(int i = ; i< ;i++)
  41. cout<< sTest[i]<<endl;
  42. return ;
  43. }

输出结果:  

  1. abc;cba;bwe;
  2.  
  3. abc
  4. bwe
  5. cba

  使用C++标准库中的string 进行字符串的拼接 排序。

  3、子串查找和提取

  4、字符串的插入和替换

字符串与数字的转换:

  1、标准库中提供了相关类对字符串和数字进行转换。

  2、字符串sstream 用于string 的转换

    2.1、<sstream> 头文件

    2.2、istringstream  字符串输入流

    2.3、ostringstream 字符串输出流

 字符串转数字、数字转字符串。

  istringstream与ostringstream 字符串流使用

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6. //string to float
  7. void stringToNumber(const string s,float& n)
  8. {
  9.  
  10. istringstream oss(s);//创建istringstream流 将字符传入istringstream流中
  11. oss>>n;//将字符s 转换为数字n
  12.  
  13. //istringstream(s)>>n;//使用istringstream 字符串输入流
  14.  
  15. }
  16.  
  17. //数字 转 字符
  18. void numberToString(const float& f, string& s)
  19. {
  20.  
  21. ostringstream oss;//创建 ostringstream 流
  22. oss << f;//将数字传入流
  23. s=oss.str();//使用流中的.str()函数获取字符串
  24. //s= ((ostringstream&)(ostringstream()<<f)).str();
  25. }
  26.  
  27. //由于实现的是float之间的相互转换。要实现int 还需要写,下面通过宏定义的方式进行修改,后期使用模块函数修改。
  28. //使用临时变量 ,由于临时变量只有一条语句的声明周期。
  29. //修改如下
  30. #define toNumber(s,n) (istringstream(s)>>n)
  31. #define toString(n) (((ostringstream&)(ostringstream()<<n)).str())
  32. int main()
  33. {
  34. float temp_val;
  35. stringToNumber("1.12",temp_val);
  36. cout << temp_val <<endl;
  37. string sTest = "";
  38. numberToString(1.22,sTest);
  39. cout <<sTest <<endl;
  40.  
  41. double val=;
  42. if(toNumber("",val))
  43. cout << val << endl;
  44. cout << toString(13.25) <<endl;
  45.  
  46. return ;
  47. }

运行结果:  

  1. 1.12
  2. 1.22
  3.  
  4. 13.25

字符串左右移动:(实现字符串的 <<  >>操作) 实现字符串的循环左右移动。  

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. //使用字符串 string_nam.substr();//字符串剪切函数
  8. //string_nam.length(); 获取当前字符串长度
  9. void testFun()
  10. {
  11. string s = "abcdef";
  12. string sOut ="";
  13. sOut = s.substr();
  14. sOut += s.substr(,);
  15. cout <<sOut <<endl;
  16. cout << s.length() <<endl;
  17. }
  18.  
  19. void testFun1( string &s,int n)
  20. {
  21. string temp ="";
  22. unsigned int mark = ;
  23. n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
  24. mark = s.length()-n;//获取字符串长度
  25.  
  26. temp = s.substr(mark);//从 第mark个数字截取后续的字符
  27.  
  28. temp += s.substr(,mark);//截取从0到mark的字符 ,完成字符移动
  29.  
  30. s = temp;
  31.  
  32. }
  33.  
  34. string operator >> (const string &s ,int n )
  35. {
  36. string temp ="";
  37. unsigned int mark = ;
  38. n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
  39. mark = s.length()-n;//获取字符串长度
  40.  
  41. temp = s.substr(mark);//从 第mark个数字截取后续的字符
  42.  
  43. temp += s.substr(,mark);//截取从0到mark的字符 ,完成字符移动
  44.  
  45. return temp;
  46. }
  47.  
  48. string operator << (const string &s ,int n )
  49. {
  50. string temp ="";
  51. unsigned int mark = ;
  52. n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
  53. mark = s.length()-n;//获取字符串长度
  54.  
  55. temp = s.substr(s.length()-mark);//从 第mark个数字截取后续的字符
  56. temp += s.substr(,s.length()-mark);//截取从0到mark的字符 ,完成字符移动
  57.  
  58. return temp;
  59. }
  60.  
  61. int main()
  62. {
  63. string sTest ="abcdefghij";
  64. cout << sTest <<endl;
  65. string sTest2 =sTest>>;
  66. cout << sTest2 <<endl;
  67. sTest2 = sTest<<;
  68. cout << sTest2 <<endl;
  69. return ;
  70. }

运行:

  

  1. abcdefghij
  2. hijabcdefg
  3. bcdefghija

C++ 标准库字符串类使用的更多相关文章

  1. 实现C++标准库string类的简单版本

    代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...

  2. C++标准库异常类

    C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报  分类: c/c++(36)  C++标准库异常类继承层次中的根类为exception,其定义在excep ...

  3. C++异常第二篇---C++标准库异常类exception的使用

    1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...

  4. [技术] OIer的C++标准库 : 字符串库<string>

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 众所周知, OI中经常用到字符串相关的处理, 这时善用字符串库可以使一些操作更加简洁易 ...

  5. [技术] OIer的C++标准库 : 字符串库

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...

  6. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  7. php标准库DirectoryIterator类的操作说明

    <?php $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileInfo) { if ($fileInf ...

  8. 把《c++ primer》读薄(3-3 标准库bitset类型)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. //开头 #include <bitset> using std::bitset; 问题1.标准库bitset类型( ...

  9. C++ 标准库概览(一分钟就看完了)

    C++ 标准库以若干头文件的方式提供. 下面简单介绍一个各头文件的内容. 第一部分 容器 Containers <array> C++11 新增.提供了容器类模板 std::array,固 ...

随机推荐

  1. win7安装ElasticSearch集群

    1.单节点安装请参考上篇博客 http://www.cnblogs.com/lianliang/p/7953754.html 2.集群的安装(这里模拟两个节点) 1)集群的安装,基于之前单节点的安装 ...

  2. 10.矩形覆盖 Java

    题目描述 我们可以用2**1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法? 思路 其实,倒数第一列要么就是1个2**1的矩形竖着放,要么就 ...

  3. Python 读文件:IOError: [Errno 0] Error

    Windows系统下,这种情况发生在读取文件,再写入过程中出现. 原因是读完文件后python不知道当前文件位置在哪里. 方法一是:在关闭文件前只做读或者写一种操作. 方法二是:在写入文件前使用fil ...

  4. lockfree buffer test

    性能测试(3): 对无锁队列boost::lockfree::queue和moodycamel::ConcurrentQueue做一个性能对比测试     版权声明:本文为博主zieckey原创文章, ...

  5. PHP 练习:租房子

    <form action="text.php" method="post"> 区域:<input type="checkbox&qu ...

  6. 对opencv读取的图片进行像素调整(1080, 1920) 1.cv2.VideoCapture(构造图片读取) 2.cv2.nameWindow(构建视频显示的窗口) 3.cv2.setWindowProperty(设置图片窗口的像素) 4.video_capture(对图片像素进行设置)

    1. cv2.VideoCapture(0) #构建视频抓捕器 参数说明:0表示需要启动的摄像头,这里也可以写视频的路径 2. cv2.nameWindow(name, cv2.WINDOW_NORM ...

  7. js evenloop

    一.宏任务 vs 微任务 1.macrotask setTimeOut . setInterval . setImmediate . I/O . 各种callback.UI渲染等 优先级: 主代码块 ...

  8. Winform使用ML.NET时无法加载 DLL“CpuMathNative”问题的解决方法

    同样的代码运行在netcore下可以,运行在winform中就出现错误: 引发的异常:“System.DllNotFoundException”(位于 Microsoft.ML.Data.dll 中) ...

  9. Vue-1:鄙人是如何开始学习的

    说实话,Vue这个东西早想学习她了.为啥呢?不是因为有多火热多好用多水嫩...而是每次面试都会问我,你会不会Vue...接下来就是突然安静的空气,,,真TM气人.所以鄙人在经历诸事之后决心一定要搞一下 ...

  10. LC 965. Univalued Binary Tree

    A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...