C++ 标准库字符串类使用
标准库中的字符串类
C++语言直接支持C语言所有概念。
C++中没有原生的字符串类型。
由于C++中没有原生的字符串类型,C++标准库提供了string类型。
1、string 直接支持字符串链接
2、字符串大小比较
/*实验 字符串排序 拼接*/
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- /*实验1 字符串排序 拼接*/
- string string_add(string s[],int addnumber)
- {
- string ret="";
- for(int i= ; i<addnumber ;i++)
- {
- ret += s[i]+';';//字符串拼接
- }
- return ret;
- }
- //首字母排序 swap();C++交换函数
- //按照首字母排序
- void string_sord(string s[],int len)
- {
- for(int i = ;i<len ; i++)
- {
- for(int j=i ;j<len;j++)
- if(s[i] > s[j])
- swap(s[i],s[j]);//使用 swap()交换两个字符串
- }
- }
- int main()
- {
- string sTest[] =
- {
- "abc",
- "cba",
- "bwe"
- };
- cout<<string_add(sTest,)<<endl;
- cout<<endl;
- string_sord(sTest,);
- for(int i = ; i< ;i++)
- cout<< sTest[i]<<endl;
- return ;
- }
输出结果:
- abc;cba;bwe;
- abc
- bwe
- cba
使用C++标准库中的string 进行字符串的拼接 排序。
3、子串查找和提取
4、字符串的插入和替换
字符串与数字的转换:
1、标准库中提供了相关类对字符串和数字进行转换。
2、字符串sstream 用于string 的转换
2.1、<sstream> 头文件
2.2、istringstream 字符串输入流
2.3、ostringstream 字符串输出流
字符串转数字、数字转字符串。
istringstream与ostringstream 字符串流使用
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- //string to float
- void stringToNumber(const string s,float& n)
- {
- istringstream oss(s);//创建istringstream流 将字符传入istringstream流中
- oss>>n;//将字符s 转换为数字n
- //istringstream(s)>>n;//使用istringstream 字符串输入流
- }
- //数字 转 字符
- void numberToString(const float& f, string& s)
- {
- ostringstream oss;//创建 ostringstream 流
- oss << f;//将数字传入流
- s=oss.str();//使用流中的.str()函数获取字符串
- //s= ((ostringstream&)(ostringstream()<<f)).str();
- }
- //由于实现的是float之间的相互转换。要实现int 还需要写,下面通过宏定义的方式进行修改,后期使用模块函数修改。
- //使用临时变量 ,由于临时变量只有一条语句的声明周期。
- //修改如下
- #define toNumber(s,n) (istringstream(s)>>n)
- #define toString(n) (((ostringstream&)(ostringstream()<<n)).str())
- int main()
- {
- float temp_val;
- stringToNumber("1.12",temp_val);
- cout << temp_val <<endl;
- string sTest = "";
- numberToString(1.22,sTest);
- cout <<sTest <<endl;
- double val=;
- if(toNumber("",val))
- cout << val << endl;
- cout << toString(13.25) <<endl;
- return ;
- }
运行结果:
- 1.12
- 1.22
- 13.25
字符串左右移动:(实现字符串的 << >>操作) 实现字符串的循环左右移动。
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- //使用字符串 string_nam.substr();//字符串剪切函数
- //string_nam.length(); 获取当前字符串长度
- void testFun()
- {
- string s = "abcdef";
- string sOut ="";
- sOut = s.substr();
- sOut += s.substr(,);
- cout <<sOut <<endl;
- cout << s.length() <<endl;
- }
- void testFun1( string &s,int n)
- {
- string temp ="";
- unsigned int mark = ;
- n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
- mark = s.length()-n;//获取字符串长度
- temp = s.substr(mark);//从 第mark个数字截取后续的字符
- temp += s.substr(,mark);//截取从0到mark的字符 ,完成字符移动
- s = temp;
- }
- string operator >> (const string &s ,int n )
- {
- string temp ="";
- unsigned int mark = ;
- n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
- mark = s.length()-n;//获取字符串长度
- temp = s.substr(mark);//从 第mark个数字截取后续的字符
- temp += s.substr(,mark);//截取从0到mark的字符 ,完成字符移动
- return temp;
- }
- string operator << (const string &s ,int n )
- {
- string temp ="";
- unsigned int mark = ;
- n = n %s.length();//防止n 超过字符串的长度。移动长度与字符串长度相等相当于不移动
- mark = s.length()-n;//获取字符串长度
- temp = s.substr(s.length()-mark);//从 第mark个数字截取后续的字符
- temp += s.substr(,s.length()-mark);//截取从0到mark的字符 ,完成字符移动
- return temp;
- }
- int main()
- {
- string sTest ="abcdefghij";
- cout << sTest <<endl;
- string sTest2 =sTest>>;
- cout << sTest2 <<endl;
- sTest2 = sTest<<;
- cout << sTest2 <<endl;
- return ;
- }
运行:
- abcdefghij
- hijabcdefg
- bcdefghija
C++ 标准库字符串类使用的更多相关文章
- 实现C++标准库string类的简单版本
代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...
- C++标准库异常类
C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报 分类: c/c++(36) C++标准库异常类继承层次中的根类为exception,其定义在excep ...
- C++异常第二篇---C++标准库异常类exception的使用
1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...
- [技术] OIer的C++标准库 : 字符串库<string>
引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 众所周知, OI中经常用到字符串相关的处理, 这时善用字符串库可以使一些操作更加简洁易 ...
- [技术] OIer的C++标准库 : 字符串库
引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- php标准库DirectoryIterator类的操作说明
<?php $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileInfo) { if ($fileInf ...
- 把《c++ primer》读薄(3-3 标准库bitset类型)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. //开头 #include <bitset> using std::bitset; 问题1.标准库bitset类型( ...
- C++ 标准库概览(一分钟就看完了)
C++ 标准库以若干头文件的方式提供. 下面简单介绍一个各头文件的内容. 第一部分 容器 Containers <array> C++11 新增.提供了容器类模板 std::array,固 ...
随机推荐
- win7安装ElasticSearch集群
1.单节点安装请参考上篇博客 http://www.cnblogs.com/lianliang/p/7953754.html 2.集群的安装(这里模拟两个节点) 1)集群的安装,基于之前单节点的安装 ...
- 10.矩形覆盖 Java
题目描述 我们可以用2**1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法? 思路 其实,倒数第一列要么就是1个2**1的矩形竖着放,要么就 ...
- Python 读文件:IOError: [Errno 0] Error
Windows系统下,这种情况发生在读取文件,再写入过程中出现. 原因是读完文件后python不知道当前文件位置在哪里. 方法一是:在关闭文件前只做读或者写一种操作. 方法二是:在写入文件前使用fil ...
- lockfree buffer test
性能测试(3): 对无锁队列boost::lockfree::queue和moodycamel::ConcurrentQueue做一个性能对比测试 版权声明:本文为博主zieckey原创文章, ...
- PHP 练习:租房子
<form action="text.php" method="post"> 区域:<input type="checkbox&qu ...
- 对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 ...
- js evenloop
一.宏任务 vs 微任务 1.macrotask setTimeOut . setInterval . setImmediate . I/O . 各种callback.UI渲染等 优先级: 主代码块 ...
- Winform使用ML.NET时无法加载 DLL“CpuMathNative”问题的解决方法
同样的代码运行在netcore下可以,运行在winform中就出现错误: 引发的异常:“System.DllNotFoundException”(位于 Microsoft.ML.Data.dll 中) ...
- Vue-1:鄙人是如何开始学习的
说实话,Vue这个东西早想学习她了.为啥呢?不是因为有多火热多好用多水嫩...而是每次面试都会问我,你会不会Vue...接下来就是突然安静的空气,,,真TM气人.所以鄙人在经历诸事之后决心一定要搞一下 ...
- 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 ...