SgtOVERKILL
SgtOVERKILL

Reputation: 319

c++ vector equivalent of this array

What is the equivalent of this array code as a vector? Random is just a random number not too important.

 Array[random - 1] = Array[random - 1] + 1;  

Below is what I'm trying and says Must have pointer to object type. I had this working a few days ago and I cannot remember how I had it.

Vector.at[random - 1] = Vector.at[random - 1] + 1;

Upvotes: 1

Views: 126

Answers (1)

sjrowlinson
sjrowlinson

Reputation: 3355

The operator[] (subscript operator) is overloaded for std::vector so you can simply do:

vec[random-1] = vec[random-1] + 1;

Or,

vec.at(random-1) = vec.at(random-1) + 1;

if you want to use at method instead.

Also, note this is equivalent to (shorter code):

vec[random-1]++;

Upvotes: 1

Related Questions