array::at()
निर्दिष्ट सूचकांक पर तत्व का संदर्भ लौटाता है। सीमा जांच करता है - अमान्य सूचकांक पर out_of_range अपवाद फेंकता है।
सिंटैक्स
array_name.at(index);
पैरामीटर
एक्सेस किए जाने वाले तत्व का सूचकांक।
रिटर्न
निर्दिष्ट सूचकांक पर तत्व।
उदाहरण 1: सामान्य उपयोग
#include <bits/stdc++.h>
using namespace std;
int main() {
array arr = {12.1, 67.3, 45.0, 89.1};
cout << "The element at index 1 is " << arr.at(1) << endl;
return 0;
}
आउटपुट
The element at index 1 is 67.3
उदाहरण 2: सीमा से बाहर सूचकांक पर अपवाद
#include <bits/stdc++.h>
using namespace std;
int main() {
array arr = {12.1, 67.3, 45.0, 89.1};
cout << "The element at index 8 is " << arr.at(8) << endl;
return 0;
}
आउटपुट
terminate called after throwing an instance of 'std::out_of_range' what(): array::at: __n (which is 8) >= _Nm (which is 4)