c++ string类基本使用
- 初始化用法
#include <iostream>
#include "string"
using namespace std;
void main()
{
string m1 = "陈培昌";
string m2("付高峰");
string m3 = m2;
cout<<"m1:"<<m1<< endl;
cout<< "m2:" << m2 << endl;
cout<< "m3:" << m3 << endl;
}
- 三种遍历方式
void main()
{
string chroums = "Deep love is a burning fire Stay";
//方法一:数组遍历
int i;
for (i=;i<chroums.length();i++)
{
cout << chroums[i];
}
cout<<endl << "==============================" << endl;
//方法二:迭代器
for (string::iterator it = chroums.begin(); it != chroums.end(); it++)
{
cout << *it;
}
string myequal(, '*');//一次性生成30个*
cout<<endl<<myequal << endl;
//方法三:at()
for (i = ; i < chroums.length(); i++)
{
cout << chroums.at(i);
}
string anotherequal(, '$');
cout << endl << anotherequal << endl;
}
输出结果:

- 选择at()方法遍历的好处----可以捕捉异常,注意示例中,故意越界访问
void main()
{
string chroums = "Deep love is a burning fire Stay";
string myequal(, '*');//一次性生成30个*
cout << endl << myequal << endl;
//方法三:at()
int i = ;
try
{
for (i = ; i < chroums.length() + ; i++)
{
cout << chroums.at(i);
}
}
catch (...)
{
cout << endl;
cout << "crisis happend" << endl;
}
system("pause");
}
输出结果:

- 而选用其他方式遍历,尽管采取措施捕捉异常,仍旧无法制止错误
#include <iostream>
#include "string"
using namespace std;
void main()
{
string chroums = "Deep love is a burning fire Stay"; //方法二:迭代器
try
{
for (string::iterator it = chroums.begin(); it != chroums.end()+; it++)
{
cout << *it;
}
}
catch (...)
{
cout << "crisis happend" << endl;
} string anotherequal(, '$');
cout << endl << anotherequal << endl;
}
输出结果:

- 查找目标字符串位置
void main()
{
string mywords = "Brother Louie, Louie, Louie";
size_t step3 = mywords.find("Louie",);//size_t是C语言int类型的一种引用
cout <<"在字符串索引处"<< step3<<"找到目标字符串" << endl;
}
输出结果:

改进:持续查找(偏移量不等于字符串的末尾)
void main()
{
string mywords = "Brother Louie, Louie, Louie";
size_t step3 = mywords.find("Louie",);//size_t是偏移量,在C语言中是int类型的引用
while (step3!=string::npos)
{
cout << "在字符串索引处" << step3 << "找到目标字符串" << endl;
step3 = step3 + ;
step3 = mywords.find("Louie", step3);
}
}
输出结果:

- 替换
void main()
{
string mywords = "徐晓冬卷了一只烤鸭饼,兀自咀嚼了起来。而陈培昌盛了一勺汤,品着陷入了沉思";
mywords.replace(,,"付高峰");
cout << mywords <<endl;
}
输出结果:

- 特别位置上的替换
void main()
{
string mywords = "付高峰卷了一只烤鸭饼,兀自咀嚼了起来。而陈培昌盛了一勺汤,品着陷入了沉思";
mywords.replace(,,"徐晓冬");
size_t cpc = mywords.find("陈培昌",);
mywords.replace(cpc,,"吴子龙");
cout << mywords <<endl;
}
输出结果:

- 截断
void main()
{
string mywords = "Brother Louie, Louie, Louie";
//size_t t = mywords.find("烤羊腿",0);
string::iterator it = find(mywords.begin(),mywords.end(),'L');
if (it!= mywords.end())
{
mywords.erase(it);
}
cout << mywords << endl;
//截断字符串erase()
}
输出:

c++ string类基本使用的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- [CF1070A]Find a Number_bfs
Find a Number 题目链接:http://codeforces.com/problemset/problem/1070/A 数据范围:略. 题解: 因为$d$和$s$比较小可以搜. 这就很$ ...
- 《Mysql - 为什么表数据删掉一半,表文件大小不变?》
一:概念 - 这里,我们还是针对 MySQL 中应用最广泛的 InnoDB 引擎展开讨论. - 一个 InnoDB 表包含两部分,即:表结构定义和数据. - 在 MySQL 8.0 版本以前,表结构是 ...
- java输入输出 -- java NIO之文件通道
一.简介 通道是 Java NIO 的核心内容之一,在使用上,通道需和缓存类(ByteBuffer)配合完成读写等操作.与传统的流式 IO 中数据单向流动不同,通道中的数据可以双向流动.通道既可以读, ...
- Xpath解析
import requests from lxml import etree url = 'https://www.huawei.com/cn/?ic_medium=direct&ic_sou ...
- IBM Security AppScan Standard使用方法
一.常规配置Appscan (安全自动化测试工具) Appscan是web应用程序渗透测试舞台上使用最广泛的工具之一.它是一个桌面应用程序,它有助于专业安全人员进行Web应用程序自动化脆弱性评估.本文 ...
- Python习题005
作业一 :任意一个数字列表,然后进行排序(冒泡排序) 方法一: def test1(): list1 = [1,23,4,6,8,55,2,9,90,35] list1.sort() # sort() ...
- asp.net core-14.JWT认证授权 生成 JWT Token
源码下载 语言组织能力不好 ,看这个 视频 用visual studio code打开文件,运行dotnet watch run 之后在postman里面去访问 拿到Token后
- css之word-wrap和word-break的区别
对于英文单词,如果有一个连写且长度很长的英文单词,在第一行显示不下的情况下,浏览器默认不会截断显示,而是把这个单词整体挪到下一行.但是当整体挪到下一行还是显示不完全该肿么办呢?有如下两个方法: wor ...
- MySQL 军规
MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...
- C#projectPropertyGrid 显示结构(展开逗号分隔)
[TypeConverter(typeof(ExpandableObjectConverter))] public A MyA { get{..} set{..} }