I\'m new to C++ and am curious if this is the preferred way of inserting into a std::vector
std::vector myVector;
void setAt(int x
If you're looking for a java-like equivalent of the ArrayList.set()
method, you can do it more closely via
void setAt(int x, Object_I_madeup o)
{
myVector.at(x) = o;
}
Much like the Java version, vector::at()
will throw an exception if the vector is not large enough. Note, this makes a copy of the object (actually, two, since you're also passing by value to the function).