当前位置:网站首页>STL string输出与修改
STL string输出与修改
2022-07-17 18:03:00 【joker_0030】
1、string输出
#include <iostream>
using namespace std;
void StrCout()
{
string str2("ojkojkojk");
cout << str2 << endl;
cout << str2.c_str() << endl;
cout << str2[0] << endl;
cout << str2.at(3)<< endl;//at()函数返回一个引用,指向在index位置的字符(下标),如果index不在字符串范围内,at()会报告“out f range”错误
//越界处理
try
{
str2.at(19);
}
/*catch (...)
{
cout << "越界了\n";
}*/
catch (out_of_range& a)
{
cout << "越界了\n";
}
}
int main()
{
StrCout();
system("pause");
return 0;
}
2、
#include <iostream>
using namespace std;
void StrCout()
{
string str2("ojkojkojk");
cout << str2 << endl;
cout << str2.c_str() << endl;
cout << str2[0] << endl;
cout << str2.at(3) << endl;//at()函数返回一个引用,指向在index位置的字符(下标),如果index不在字符串范围内,at()会报告“out f range”错误
//越界处理
try
{
str2.at(19);
}
/*catch (...)
{
cout << "越界了\n";
}*/
catch (out_of_range& a)
{
cout << "越界了\n";
}
}
void StrChange()
{
string str2("ojkojkojk");//改
/*str2[2] = 's';
str2.at(3) = 'q';//替换*/
//string str3(5, 'q');//增
string str3("abcdefg");//insert 函数。插入
//str2.insert(2, str3);//将str3插入下标为2的位置。
//str2.insert(2, "akfdsks");//将字符串插入下标为2的位置。
//str2.insert(3, str3,0,2);//将str3中下标为[0,2)插入str2中的下标为3的位置。
//str2.insert(3,"sjhdfja", 2);//将字符串中下标为[0,2)插入str2中的下标为3的位置。
//str2.insert(3, 2, 'b');// 在str2的下标为3的位置插入2个b。
//str2 += str3;//末尾加入。//增
//str2 += "sjhasd";//末尾加入。
//append函数//末尾加入,与insert近似,插入的是末尾。//增
//str2.append(str3);
//str2.append("sdhasjj",3);//前三个加到str2后
//str2.append(3,'w');//将三个w加到str2后
//重新赋值//改
//str2 = "qwer";
//str2 = str3;
//cin>>str2;
//str2.assign("ashjd", 2);//与insert和append用法相同。
//删除
//str2.erase(2,3);//在str2中从下标为2开始删除3个字符。
str2.erase(0,str2.length());//删除所有字符。
cout << str2 << endl;
}
int main()
{
//StrCout();
StrChange();
system("pause");
return 0;
}
边栏推荐
- 使用case语句时会产生锁存器的情况
- Possible problems in inserting Excel data into MySQL database
- 响应式织梦模板物流货运服务类网站
- 面试难题:分布式 Session 实现难点,这篇就够!
- [C language programming 8] branch predictor
- AE如何制作星云粒子特效
- 响应式织梦模板酒窖类网站
- 可视化ETL工具Kettle概念、安装及实战案例
- [C language programming 7] BTB model
- Growth of operation and maintenance Xiaobai - week 6 of Architecture
猜你喜欢

Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
![[pyGame learning notes] 6 Cursor mouse cursor](/img/ea/70fb3043ae32eca68e31144a54b651.jpg)
[pyGame learning notes] 6 Cursor mouse cursor

时序逻辑与组合逻辑的reg

Fluorine modified uio-66 | 3,4-dihydroxybenzaldehyde modified uio-66-nh2 | camptothecin derivative / oligopeptide @zif-8 nano drug carrier system

LeetCode 0565. Array nesting: convert to graph + modify in place の optimization

2022年最新吉林建筑安全员模拟题库及答案

VMware imports ova/ovf virtual machine files

【Pygame 学习笔记】6.Cursor 鼠标光标
[email protected] Support) | uio-66/coso composite | zif-67 nanocrystalline sur"/>Copper sulfide nanoparticles /zif-8 Composites( [email protected] Support) | uio-66/coso composite | zif-67 nanocrystalline sur

Is the career direction of test / development programmers over 35 a turning point in the workplace?
随机推荐
Is it safe for Everbright futures to open an account online? Are there any account opening guidelines?
CMOS switch learning (I)
R语言--Cox模型校准曲线原理(一)数据来源
【腾讯蓝鲸】第七届 7·24 运维日节日祝福送上~ 快来许愿~
Reg of sequential logic and combinatorial logic
Is the career direction of test / development programmers over 35 a turning point in the workplace?
响应式织梦模板物流货运服务类网站
Advanced C language -- custom type: structure enumeration Union
可视化ETL工具Kettle概念、安装及实战案例
Attachment handling of SAP Fiori
mysql排序索引失效?
npm err! [email protected] build: `umi build`
面试官:可以接受转Go吗?
npm err! ant-design- [email protected] build: `umi build`
Interview difficulties: difficulties in implementing distributed session, this is enough!
Flask源码分析(三):上下文
Cobalt iron bimetallic organic skeleton cox/mil-100 (FE) | [email protected]
Wrong again, byte alignment and the use of pragma pack
MYCAT divides the database and table according to the hash value of the string range
XML建模(简单易学)