C++-容器-string:从string字符串中获取最后一个字符【std::string::back()】【std::string::at(size - 1)】

本文介绍在C++中从字符串中获取最后一个字符的方法,包括使用std::string::at、std::string::back、std::string::rbegin、std::string::begin结合reverse以及数组索引等五种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将教授 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() 给出了对字符串最后一个字符的引用。这仅适用于非空字符串。

此函数还可以替换字符串末尾的字符。

语法:

  1. 访问最后一个字符。
char ch = string_name.back();
  1. 替换出现在字符串末尾的字符。
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
`std::string` 在 C++ 中是一个标准库中的字符序列容器,可以用来处理字符串操作。如果想通过 `::` 分隔字符串获取倒数第二个子字符串,通常需要先使用 `find` 或 `substr` 函数来找到特定的分隔符位置,然后根据这些位置进行切割。 以下是一个示例: ```cpp #include <iostream> #include <string> std::string get_substring(const std::string &str, char delimiter) { size_t pos = str.find(delimiter); // 查找第一个分隔符的位置 if (pos == std::string::npos) { // 如果没有找到,返回整个字符串(默认情况) return str; } std::vector<std::string> parts; // 存储分割后的部分 parts.push_back(str.substr(0, pos)); // 获取第一个部分 while (true) { pos = str.find(delimiter, pos + 1); // 在剩余部分查找下一个分隔符 if (pos == std::string::npos) { // 找不到更多分隔符了,结束循环 break; } parts.push_back(str.substr(pos + 1)); // 获取新的部分,跳过分隔符 } return parts[parts.size() - 2]; // 返回倒数第二个子字符串 } int main() { std::string input = "parent::child::grandchild"; char delimiter = '::'; std::cout << "The second-to-last substring is: " << get_substring(input, delimiter) << std::endl; return 0; } ``` 在这个例子中,`get_substring` 函数会从给定的 `str` 开始,每次查找 `delimiter` 并将其后面的子串添加到结果列表中,直到找不到更多的分隔符为止。最后返回列表中的倒数第二个元素,即倒数第二个子字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值