博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
stl取出字符串中的字符_从C ++ STL中的字符串访问字符元素
阅读量:2532 次
发布时间:2019-05-11

本文共 1517 字,大约阅读时间需要 5 分钟。

stl取出字符串中的字符

字符串作为数据类型 (String as datatype)

In C, we know string basically a character array terminated by \0. Thus to operate with the string we define character array. But in C++, the standard library gives us the facility to use the string as a basic data type like an integer. But the string characters can be still accessed considering the string as a character array.

在C语言中,我们知道字符串基本上是一个以\ 0结尾的字符数组。 因此,要对字符串进行操作,我们定义了字符数组。 但是在C ++中,标准库为我们提供了将字符串用作基本数据类型(如整数)的便利。 但是,仍可以将字符串视为字符数组来访问字符串字符。

Example:

例:

Like we define and declare,    string s="IncludeHelp";    s[0] = 'I' (not "I")    s[7] = 'H'         //So same way we can access the string element which is character.    //Also there is a function under string class, at(),     //which can be used for the same purpose.    cout << s.at(7); //prints H

Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.

请记住,需要在“”下定义一个字符串变量(文字)。 “ a”是字符,而“ a”是字符串。

Note: Trying to accessing character beyond string length results in segmentation fault.

注意:尝试访问超出字符串长度的字符会导致分段错误。

Header file needed:

所需的头文件:

#include 
Or #include

C++ program to demonstrate example of accessing characters of a string

C ++程序演示访问字符串字符的示例

#include 
using namespace std;int main(){
string s; cout<<"enter string...\n"; cin>>s; cout<<"Printing all character elements...\n"; for(int i=0;i

Output

输出量

enter string...IncludeHelpPrinting all character elements...I n c l u d e H e l p

翻译自:

stl取出字符串中的字符

转载地址:http://afazd.baihongyu.com/

你可能感兴趣的文章
Java 构造方法、构造方法重载、复制构造方法
查看>>
创建可重用的对象
查看>>
在CentOS构造内核树----通过yum方式(智能化方式)
查看>>
zabbix如何监控WEB应用性能
查看>>
软件工程——理论、方法与实践 第九章
查看>>
mac环境下mongodb的安装和使用
查看>>
实验9 根据材料编程
查看>>
网站架构
查看>>
Request.QueryString 和 Request.Param的区别
查看>>
CSS技巧(一):清除浮动
查看>>
狄利克雷分布
查看>>
atitit. 研发管理---如何根据自己的特挑选 产业、行业、职业、岗位与自己发展的关系...
查看>>
Atitit. WordPress 4.2.2新特性对比 attilax总结
查看>>
Atitit.常用分区api的attilax总结
查看>>
atitit。ocr框架类库大全 attilax总结
查看>>
EventDispatcher 事件分发组件
查看>>
JavaScript表格隔行换色悬停高亮
查看>>
博客园,我又回来了
查看>>
adt-bundle-eclipse下的修改android项目名
查看>>
Unity-UGUI相关
查看>>