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,固 ...
随机推荐
- python中的fstring的 !r,!a,!s
首先是fstring的结构 f ' <text> { <expression> <optional !s, !r, or !a> <optional : fo ...
- 05.用两个栈实现队列 Java
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 进栈: 直接进stack1 出栈: 若stack2不为空,则出栈. 否则,当stack1不为空时, ...
- IP输出 之 ip_output、ip_finish_output、ip_finish_output2
概述 ip_output-设置输出设备和协议,然后经过POST_ROUTING钩子点,最后调用ip_finish_output: ip_finish_output-对skb进行分片判断,需要分片,则分 ...
- SpringBoot2.X 静态文件配置
Spring Boot 默认会挨个从 META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回. 默认配置 ...
- Ceph RBD 的实现原理与常规操作
目录 文章目录 目录 前文列表 RBD RBD Pool 的创建与删除 块设备的创建与删除 块设备的挂载与卸载 新建客户端 块设备的扩缩容 RBD 块设备的 Format 1 VS Format 2 ...
- 在 Ubuntu 14.10 Server 上安装 Jetty
Jetty提供了一个Web服务器和javax.servlet容器,为SPDY.WebSocket.OSGi.JMX.JNDI.JAAS以及许多其它集成套件添加了支持.这些组件都是开源的,也可用于商业用 ...
- Linux 查看操作系统版本信息 uname
Linux 查看操作系统版本信息 uname uname 命令用于显示当前系统的版本信息. 带 -a 选项的 uname 命令会给出当前操作系统的所有有用信息. 命令如下: [root@node1 / ...
- ubuntu下wps的安装
(一)安装 1)下载:WPS For Linux http://community.wps.cn/download/ 下载wps-office_10.1.0.5672~a21_amd64.deb 2) ...
- django helloworld
http://note.youdao.com/noteshare?id=8f0b036922e71c1feb5d0d06a4779c6f
- Laravel底层原理系列
Laravel 从学徒到工匠精校版 地址:https://laravelacademy.org/laravel-from-appreciate-to-artisan Advanced Applicat ...