获取std :: string的最后一个元素 [英] Get the last element of a std::string
本文介绍了获取std :: string的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一个缩写或更优雅的方式获取字符串的最后一个字符,如:
I was wondering if there's an abbreviation or a more elegant way of getting the last character of a string like in:
char lastChar = myString.at( myString.length() - 1 );
像 myString.back()
似乎存在。是否有等价的?
Something like myString.back()
doesn't seem to exist. Is there an equivalent?
推荐答案
在C ++ 11中,可以使用 c $ c>成员函数:
In C++11, you can use the back
member function:
char ch = myStr.back();
在C ++ 03, std :: string :: back
由于疏忽而无法使用,但您可以通过取消引用
reverse_iterator
从 rbegin
:
In C++03, std::string::back
is not available due to an oversight, but you can get around this by dereferencing the reverse_iterator
you get back from rbegin
:
char ch = *myStr.rbegin();
希望这有助于!
这篇关于获取std :: string的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文