Difference Between courseGrades[i] and courseGrades.at(i)

  • Context: MHB 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
Click For Summary
SUMMARY

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.

PREREQUISITES
  • Understanding of C++ vectors
  • Knowledge of C++ exception handling
  • Familiarity with array indexing in programming
  • Basic syntax of C++ programming language
NEXT STEPS
  • Explore C++ vector methods and their performance implications
  • Learn about exception handling in C++
  • Research best practices for accessing elements in C++ containers
  • Study the differences between C++ arrays and vectors
USEFUL FOR

C++ developers, software engineers, and students learning about data structures and error handling in C++ programming.

ineedhelpnow
Messages
649
Reaction score
0
i may have asked this before but what is the difference between and at(i)?

ex. courseGrades and courseGrades.at(i)
 
Technology news on Phys.org
ineedhelpnow said:
i may have asked this before but what is the difference between and at(i)?

ex. courseGrades and courseGrades.at(i)


courseGrades is element number i of the array courseGrades kindly remember that element number starts at 0

where as courseGrades.at(i) is call to the function courseGrades.at with parameter i
 
so you can't call a function using courseGrades?
 
ineedhelpnow said:
so you can't call a function using courseGrades?


Sure you can.
If courseGrades is a vector, you can call a function with courseGrades as a parameter. (Wasntme)
 
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??


The only difference is in general at(i) throws an exception if you attempt to access the vector at an out of bounds index, while does not (and just triggers undefined behaviour instead, but as a result cannot be slower than at(i)). Operators are just functions with a bit of syntactic sugar thrown on top of them, don't worry too much about operators looking different.
 
ineedhelpnow said:
im confused. do you use to check through all the items in the list??


Yes... although I'm not sure if I understand your question. :confused:
 
can someone please show me an example of that when to use and when to use at(i)? :o like a single line of code or something. it would make it a lot more clear...
 
ineedhelpnow said:
can someone please show me an example of that when to use and when to use at(i)? :o like a single line of code or something. it would make it a lot more clear...


You can use either whenever you want.
It's merely a matter of personal preference.
The one distinction there is, that relates to exceptions, is in practice not particularly relevant.

So you can do either:
Code:
#include <vector>
std::vector<int> v;

for (int i = 0; i < v.size(); ++i) {
  cout << v[i] << ", ";
}

or:
Code:
for (int i = 0; i < v.size(); ++i) {
  cout << v.at(i) << ", ";
}
(Wasntme)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
8K
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
2K
Replies
86
Views
2K
Replies
1
Views
2K
  • · Replies 42 ·
2
Replies
42
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
5K
Replies
2
Views
3K