C++-容器-string:从string字符串中获取最后一个字符【std::string::back()】【std::string::at(size - 1)】
本文将教授 C++ 中从字符串中获取最后一个字符的不同方法。
在 C++ 中使用 std::string::at 从字符串中获取最后一个字符
我们可以使用 std::string::at
从字符串中提取出现在给定位置的字符。
语法:
char& string::at(size_type indexNo)
这将返回用户指定的 indexNo
处指定位置的字符。当传递了无效的索引号时,该方法会抛出 out_of_range
异常,例如小于零的索引或大于或等于 string.size()
的索引。
示例:提取最后一个字符和给定位置的字符。
#include <iostream>
using namespace std;
int main()
{
string str("Spider Man");
cout<<"Character at position 3: "<<str.at(3)<<endl;
int length = str.size();
cout<<"Character at last position: "<<str.at(length-1)<<endl;
}
<div class="panel panel-primary panel-warning">
<div class="panel-heading">解释</div>
<div class="panel-body">
为了得到最后一个位置的字符,我们首先找到字符串的长度,并在 std::string::at()
方法中传递 length-1
。
输出:
Character at position 3: d
Character at last position: n
在 C++ 中使用 std::string::back() 从字符串中获取最后一个字符
C++ 中的 std::string::back()
给出了对字符串最后一个字符的引用。这仅适用于非空字符串。
此函数还可以替换字符串末尾的字符。
语法:
- 访问最后一个字符。
char ch = string_name.back();
- 替换出现在字符串末尾的字符。
str.back() = 'new_char_we_to_replace_with';
此函数不带参数,它提供对字符串最后一个字符的引用。当应用于空字符串时,它会显示未定义的行为。
示例代码:
#include <iostream>
using namespace std;
int main()
{
string str("Spider Man");
char ch = str.back();
cout<<"Character at the end "<<ch<<endl;
str.back() = '@';
cout<<"String after replacing the last character: "<<str<<endl;
}
输出:
Character at the end n
String after replacing the last character: Spider Ma@
使用 std::string::rbegin() 从 C++ 中的字符串中获取最后一个字符
C++ 中的 std::string::rbegin()
代表反向开始。该函数用于指向字符串的最后一个字符。
语法:
reverse_iterator it = string_name.rbegin();
该函数不包含任何参数,它返回指向字符串最后一个字符的反向迭代器。
示例代码:
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str("Spider Man");
string::reverse_iterator it = str.rbegin();
cout<<"The last character of the string is: "<<*it;
}
输出:
The last character of the string is: n
在 C++ 中使用 std::string:begin() 和 reverse() 从字符串中获取最后一个字符
C++ 中的 std::string::begin()
将迭代器返回到字符串的开头,不带参数。
语法:
string::iterator it = string_name.begin();
std::string::reverse()
是 C++ 中用于直接反转字符串的内置函数。它将 begin
和 end
迭代器作为参数。
该函数在 algorithm
头文件中定义。
语法:
reverse(string_name.begin(), string_name.end());
要从字符串中获取最后一个字符,我们首先使用 reverse
方法反转字符串,然后使用 begin()
方法获取字符串第一个字符的引用(这是反转之前的最后一个字符)并打印它。
示例代码:
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str("Spider Man");
reverse(str.begin(),str.end());
string::iterator it = str.begin();
cout<<"The last character of the string is: "<<*it;
}
输出:
The last character of the string is: n
在 C++ 中使用数组索引访问字符串的字符
我们可以通过使用它的索引号引用它并在方括号 []
中传递它来访问字符串的字符。
示例代码:
#include <iostream>
using namespace std;
int main()
{
string str("Bruce banner");
int length = str.size();
cout<<"The last character of the string is "<<str[length-1];
}
为了得到最后一个字符,我们首先找到字符串的长度并在方括号中传递 length-1
。
输出:
The last character of the string is r
相关文章
- C++ 获取 string 中某个字符首次出现的位置
- 2048控制台程序:一份帝国理工C++作业
- C++、C++-OpenCV、Python、Python-Numpy、MATLAB的除法取余(余数)方法总结
- 79 C++ - set/multiset容器
- 《C++程序设计教程(第3版)》——第1章,第1节计算机语言与程序
- c++中字符数组内存和指针问题示例解答
- 纯C++创建Windows窗体(理解WinMain()与WndProc())
- 《Imperfect C++中文版》——2.2 控制你的客户端
- 《c++语言导学》——1.4 函数
- android使用C/C++调用SO库
- Visual studio C++桌面应用程序添加外部文件引用
- C++刷题的时候遇到的坑
- 39、【查找】二分查找:数的范围和查找某个数(C/C++版)
- Lua 与C/C++ 交互系列:注冊枚举enum到Lua Code中
- effective c++ 条款7
- C/C++ 视频播放器开发