ineedhelpnow
- 649
- 0
i may have asked this before but what is the difference between and at(i)?
ex. courseGrades and courseGrades.at(i)
ex. courseGrades and courseGrades.at(i)
The discussion clarifies the difference between accessing elements in a C++ vector using the subscript operator courseGrades[i] and the member function courseGrades.at(i). The subscript operator does not perform bounds checking and may lead to undefined behavior if the index is out of range, while at(i) throws an exception in such cases. Both methods can be used interchangeably for accessing elements, but at(i) is safer due to its error handling. Ultimately, the choice between the two is a matter of personal preference.
C++ developers, software engineers, and students learning about data structures and error handling in C++ programming.
ineedhelpnow said:i may have asked this before but what is the difference between and at(i)?
ex. courseGrades and courseGrades.at(i)
ineedhelpnow said:so you can't call a function using courseGrades?
ineedhelpnow said:im confused. do you use to check through all the items in the list??
ineedhelpnow said:im confused. do you use to check through all the items in the list??
ineedhelpnow said:can someone please show me an example of that when to use and when to use at(i)?like a single line of code or something. it would make it a lot more clear...
#include <vector>
std::vector<int> v;
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ", ";
}
for (int i = 0; i < v.size(); ++i) {
cout << v.at(i) << ", ";
}