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

  • MHB
  • Thread starter ineedhelpnow
  • Start date
In summary, the difference between [i] and at(i) is that at(i) throws an exception if you attempt to access the vector at an out of bounds index, while [i] does not.
  • #1
ineedhelpnow
651
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
  • #2
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
 
  • #3
so you can't call a function using courseGrades?
 
  • #4
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)
 
  • #5
im confused. do you use to check through all the items in the list??
 
  • #6
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.
 
  • #7
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:
 
  • #8
can someone please show me an example of that when to use and when to use at(i)? :eek: like a single line of code or something. it would make it a lot more clear...
 
  • #9
ineedhelpnow said:
can someone please show me an example of that when to use and when to use at(i)? :eek: 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)
 

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

1. What is the difference between courseGrades[i] and courseGrades.at(i)?

The main difference is the way they handle index out of bounds errors. courseGrades[i] will not throw an error if the index is out of bounds, instead it will return whatever value is stored in that memory location. courseGrades.at(i), on the other hand, will throw an exception if the index is out of bounds.

2. Which one should I use when accessing an element in an array?

It is generally safer to use courseGrades.at(i) as it will help prevent unexpected errors due to out of bounds index. However, if you are confident that the index will always be within bounds, you can use courseGrades[i] for a slightly more efficient code.

3. Can I use courseGrades.at(i) for non-array containers?

No, courseGrades.at(i) is only applicable for containers such as arrays, vectors, and strings. For non-array containers, you can use their respective methods for accessing elements.

4. Are there any performance differences between courseGrades[i] and courseGrades.at(i)?

Yes, there is a slight difference in performance as courseGrades[i] does not do any bounds checking, making it slightly faster. However, this performance difference is usually negligible and using courseGrades.at(i) is considered safer.

5. Can I use courseGrades.at(i) to assign values to an element in an array?

Yes, you can use courseGrades.at(i) to both access and assign values to an element in an array. However, if you are not modifying the value, it is recommended to use courseGrades[i] instead for better performance.

Back
Top