queue/vectors

closed account (jGAShbRD)
i have a vector<T> storage that is a container, lets say inside i got 1,2,3,4,5 with head at 1 and end at 5. i want to remove 1 but i dont want to use .erase instead i want to remove 1 and have head be 2. also how do i add to end of vector assuming that once again i have " " 2, 3,4,5 and 2 is head and 5 is end i want to have 6 added inplace of "" and have end point to 6 how do i do this? can someone explain.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
   vector<int> V{ 1, 2, 3, 4, 5 };
   for ( auto e : V ) cout << e << " ";   cout << '\n';

   rotate( V.begin(), V.begin()+1, V.end() );
   V.back() = 6;
   for ( auto e : V ) cout << e << " ";   cout << '\n';
}
closed account (jGAShbRD)
what if like our vector has 5 elements but we want to add a 6th element, and the vector container would double to hold 10 elements. from what i learn it would have 1,2,3,4,5,6, and the rest of the vecotr would contain smething to hold how would we do it that way?
If you simply want to add something at the end use
V.push_back( something );

if you want to change the length to 10 (with accessible elements) use
V.resize( 10 );
closed account (jGAShbRD)
i know what you men but i meant like, im creating a class that does these for me
so like wen u say add to the end i also thought of pushback as best way but then i have to consider the fact that lets say we have a given vector with first element as 1 then 2 3 4 5 and last element is 5. lets say previously i had a function that removes the first element of the vector so 1 is remove as well as set the first element to 2. so this vector would be _ 2 3 4 5 the _ is the previous first element before removed. now i want to add a elelement in place of the _ lets say 6. if i do pushback it would be _ 2 3 4 5 6 _ _ _ _ _ . where _ are shows the size of the vector. but i want it to be 6 2 3 4 5 . its like a cirualr queue but in vector container.
closed account (jGAShbRD)
bearing in mind that there is a head is pointing to first element of container and end is pointing to last element of container.
LMAO.

You can use insert:

http://www.cplusplus.com/reference/vector/vector/insert/

Also, the vector's size is dynamic, it's as big as it needs to be to hold the elements. Behind the scenes, the capacity of the vector may be larger than you need, but using .size() will get you the amount of elements you can access.
Topic archived. No new replies allowed.