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

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

Discussion Overview

The discussion revolves around the differences between accessing elements in a vector using the subscript operator [i] and the member function at(i). Participants explore the implications of each method in terms of functionality and safety, particularly in the context of C++ programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that courseGrades[i] accesses the element at index i of the array, while courseGrades.at(i) is a function call with i as a parameter.
  • There is a question about whether a function can be called using courseGrades[i], to which some participants affirm that it can be used as a parameter in function calls.
  • One participant mentions that at(i) throws an exception for out-of-bounds access, whereas [i] does not and may lead to undefined behavior.
  • Another participant expresses confusion about using [i] to iterate through all items in the list, indicating a lack of clarity on the topic.
  • Examples are requested to illustrate when to use [i] versus at(i), with one participant stating that both can be used interchangeably based on personal preference.

Areas of Agreement / Disagreement

Participants express differing views on the implications of using [i] versus at(i), particularly regarding safety and error handling. There is no consensus on a definitive preference or best practice.

Contextual Notes

Participants highlight that the distinction between the two methods may not be particularly relevant in practice, but the differences in error handling are noted as significant.

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
3K
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