Hi,
new to vectors.. I have a little problem with them - with arrays you could just do like array [ index ] = new value, and everything is ok. How it is with vectors ? how can i change specific index value ?
Hi,
new to vectors.. I have a little problem with them - with arrays you could just do like array [ index ] = new value, and everything is ok. How it is with vectors ? how can i change specific index value ?
It works the same way with vectors. But since vectors grow dynamically, be extra careful that the index isn't out of bounds.
std::vector 101: (for vector v)
- Use v[ index ] to read or write any value at "index" position.
- Use v.begin() and v.end() to get an iterator at the beginning or end of v.
- Use v.resize( newSize ) to change the number of elements in v to "newSize".
- Use v.size() to know the number of elements in v.
- Use v.empty() to know if the vector v is empty or not (empty -> size() == 0)
- Use v.push_back( newElem ) to add an element at the end of the vector v.
The above is enough to make it match the capabilities of standard C-style arrays. You can learn more features from here.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.