返回對指定位置pos處字符的引用。該函數自動檢查pos是否是字符串中字符的有效位置(即pos是否小於字符串長度),如果不是,則拋出out_of_range異常。
用法:
reference at (size_type pos); const_reference at (size_type pos) const; 參數: pos - position of the character to return Return value: Reference to the requested character Exceptions: Throws std::out_of_range if pos >= size().
// CPP program to access a character through
// std::basic_string::at
#include <stdexcept>
#include <iostream>
int main()
{
// String with valid indices from 0 to 2
std::string str = "abc";
// Printing size of string
std::cout << "string size = " << str.size() << '\n';
// Accessing out of bounds index
try
{
str.at(4) = 't';
}
// If error is generated, it is caught
catch (std::out_of_range const& error)
{
std::cout << error.what() << '\n';
}
}
輸出:
string size = 3 basic_string::at:__n (which is 4) >= this->size() (which is 3)
相關用法
- Java HijrahDate getEra()用法及代碼示例
- Java HijrahDate getChronology()用法及代碼示例
- Java HijrahDate equals()用法及代碼示例
- Java HijrahDate atTime()用法及代碼示例
- Java HijrahChronology range()用法及代碼示例
- Java HijrahChronology zonedDateTime(TemporalAccessor)用法及代碼示例
注:本文由純淨天空篩選整理自 std::basic_string::at in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。