java - C++ equivalent of Arraylist.get(index) -
i have been wanting learn c++ out of interest , start of wanted see if translate of java code had written solve project euler problems(i had followed course while wanted see remembered). came across java code:
while (x != 0) { if(x%10 != 0) { digits.add(x%10); } x /= 10; }
which translated to:
std::vector<int> v; while(x != 0){ v.push_back(x%10); x /= 10; }
with of internet. question is, how value out of ¨list¨. in java can digits.get(index), how 1 in c++?
you use operator[](std::size_t)
, plain array:
int n = v[index];
note performs no bounds checking. if want bounds-checked access, use at()
member function:
int n = v.at(index);
the latter throws std::out_of_range
exception out of bounds access.
Comments
Post a Comment