C++字符串string类常用操作详解(一)【初始化、遍历、连接】
代码示例:
- #include <iostream>
- #include "string"
- using namespace std;
- //字符串初始化
- void strInit()
- {
- cout << "字符串初始化:" <<endl;
- string s1 = "abcdefg"; //初始化方式1
- string s2("abcdefg"); //初始化方式2
- string s3 = s2; //通过拷贝构造函数 初始化s3
- string s4(7,'s'); //初始化7个s的字符串
- cout << "s1 = "<< s1 << endl;
- cout << "s2 = "<< s2 << endl;
- cout << "s3 = "<< s3 << endl;
- cout << "s4 = "<< s4 << endl;
- }
- //字符串遍历
- void strErgo()
- {
- cout << "字符串遍历:" <<endl;
- string s1 = "abcdefg"; //初始化字符串
- //通过数组方式遍历
- cout << "1、通过数组方式遍历:" <<endl;
- for (int i = 0; i < s1.length(); i++)
- {
- cout << s1[i] << " ";
- }
- cout << endl;
- //通过迭代器遍历
- cout << "2、通过迭代器遍历:" <<endl;
- for(string::iterator it = s1.begin(); it!= s1.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- //通过at()方式遍历
- cout << "3、通过at()方式遍历:" <<endl;
- for (int i = 0; i < s1.length(); i++)
- {
- cout << s1.at(i) << " "; //此方式可以在越界时抛出异常
- }
- cout << endl;
- }
- //字符指针和字符串的转换
- void strConvert()
- {
- cout << "字符指针和字符串的转换:" <<endl;
- string s1 = "abcdefg"; //初始化字符串
- cout << "string转换为char*:" <<endl;
- //string转换为char*
- cout << s1.c_str() <<endl; //s1.c_str()即为s1的char *形式
- cout << "char*获取string内容:" <<endl;
- //char*获取string内容
- char buf[64] = {0};
- s1.copy(buf, 7);//复制7个元素
- cout << buf <<endl;
- }
- //字符串连接
- void strAdd()
- {
- cout << "字符串连接:" <<endl;
- cout << "方式1:" <<endl;
- string s1 = "123";
- string s2 = "456";
- s1 += s2;
- cout << "s1 = "<< s1 << endl;
- cout << "方式2:" <<endl;
- string s3 = "123";
- string s4 = "456";
- s3.append(s4);
- cout << "s3 = "<< s3 << endl;
- }
- int main()
- {
- //初始化
- strInit();
- cout << endl;
- //遍历
- strErgo();
- cout << endl;
- //字符指针类型和字符串转换
- strConvert();
- cout << endl;
- //字符串连接
- strAdd();
- cout << endl;
- system("pause");
- return 0;
- }
程序运行结果:
- 字符串初始化:
- s1 = abcdefg
- s2 = abcdefg
- s3 = abcdefg
- s4 = sssssss
- 字符串遍历:
- 1、通过数组方式遍历:
- a b c d e f g
- 2、通过迭代器遍历:
- a b c d e f g
- 3、通过at()方式遍历:
- a b c d e f g
- 字符指针和字符串的转换:
- string转换为char*:
- abcdefg
- char*获取string内容:
- abcdefg
- 字符串连接:
- 方式1:
- s1 = 123456
- 方式2:
- s3 = 123456
- 请按任意键继续. . .
C++字符串string类常用操作详解(一)【初始化、遍历、连接】的更多相关文章
- Linux Shell数组常用操作详解
Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...
- String类内存空间详解
java.lang.String类内存问题详解 字符串理解的难点在于其在堆内存空间上的特殊性,字符串String对象在堆内存上有两种空间: 字符串池(String pool):特殊的堆内存,专门存放S ...
- String类的构造方法详解
package StringDemo; //String类的构造方法详解 //方法一:String(); //方法二:String(byte[] bytes) //方法三:String (byte[] ...
- java中的String类常量池详解
test1: package StringTest; public class test1 { /** * @param args */ public static void main(String[ ...
- 【python+selenium的web自动化】- 元素的常用操作详解(一)
如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html 本篇主要内容:1.元素 ...
- 【Git使用详解】Egit的常用操作详解
常用操作 操作 说明 Fetch 从远程获取最新版本到本地,不会自动merge Merge 可以把一个分支标签或某个commit的修改合并现在的分支上 Pull 从远程获取最新版本并merge到本地相 ...
- jQuery 源码分析(十四) 数据操作模块 类样式操作 详解
jQuery的属性操作模块总共有4个部分,本篇说一下第3个部分:类样式操作部分,用于修改DOM元素的class特性的,对于类样式操作来说,jQuery并没有定义静态方法,而只定义了实例方法,如下: a ...
- pandas常用操作详解——info()与descirbe()
概述 df.info():主要介绍数据集各列的数据类型,是否为空值,内存占用情况: df.describe(): 主要介绍数据集各列的数据统计情况(最大值.最小值.标准偏差.分位数等等). df.in ...
- pandas常用操作详解——pd.concat()
concat函数基本介绍: 功能:基于同一轴将多个数据集合并 pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=Fa ...
随机推荐
- PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
- 从问题域看hadoop的各种技术
近些年来Hadoop生态系统发展迅猛,它本身包含的软件越来越多,同时带动了周边系统的繁荣发展.尤其是在分布式计算这一领域,系统繁多纷杂,时不时冒出一个系统,号称自己比MapReduce或者Hive高效 ...
- js 面向对象 继承机制
根据w3cschool上的描述:共有3种继承方法(对象冒充,原型链,混合) 1.对象冒充:构造函数ClassA使用this关键字给所有属性和方法赋值,使ClassA构造函数成为ClassB的方法,调用 ...
- JSP、Servlet中get请求和post请求的区别总结
在学习JavaWeb最初的开始阶段,大家都会遇到HttpServlet中的doGet和doPost方法.前两天看<Head First Servlets & JSP>看到其中讲关于 ...
- Android-AndroidStudio莫名其妙的错误-finished with non-zero exit value 1
上一篇博客,Android-AndroidStudio莫名其妙的错误-finished with non-zero exit value 1,解决了由于 string.xml 字符导致的: 而这篇博客 ...
- Excel 帮助无法正常工作的解决方法
Excel 中出现错误:帮助无法正常工作,但您仍可以转到 Office.com,以获得最新和最好的文章.视频和培训课程. 英文消息:Help isn't working, but you can st ...
- 自适应XAML布局经验总结 (三) 局部布局设计模式2
本系列对实际项目中的XAML布局场景进行总结,给出了较优化的自适应布局解决方案,希望对大家有所帮助. 下面继续介绍局部布局设计模式. 5. 工具箱模式 绘图,三维模型操作等需要工具的情况,可以使用带分 ...
- Checkpoint--在Tempdb上的特殊性
由于Checkpoint的目的是为减少数据库恢复时间,而每次实例重启都会创建新的tempdb,而不需要恢复,因此checkpoint在Tempdb上行为与其他用户数据库上略微不同. 1. 系统引发的c ...
- C#实现FTP文件的上传、下载功能、新建目录以及文件的删除
本来这篇博文应该在上周就完成的,可无奈,最近工作比较忙,没有时间写,所以推迟到了今天.可悲的是,今天也没有太多的时间,所以决定给大家贴出源码,不做详细的分析说明,如果有不懂的,可以给我留言,我们共同讨 ...
- C#DataGridView的简单使用
首先创建一个DataGridView控件,然后创建列(包括列名的定义), 由于我不是和数据库进行连接,只是为了输出好看一点. 删除所有数据: while (this.dataGridView1.Row ...