Removing elements from vectors the long way

Dec 11, 2014 at 1:19am
I want to know how to remove an element from a vector without changing the sequence of the other elements in the process.

This code keeps giving me the message "vector subsript out of range".

I can't use functions like v.erase. I only want to shift the desired element to the end of the vector and delete it without moving the end element to where the first element was.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  vector<string> vector;
	vector.push_back("x");
	vector.push_back("y");
	vector.push_back("z");
	vector.push_back("a");
	vector.push_back("b");
	vector.push_back("c");
	vector.push_back("d");
	vector.push_back("f");
	vector.push_back("g");
	vector.push_back("h");
	int i = 2;
	int z = 0;
	for (int x = i + 1; x < vector.size(); x++)
	{
		vector[x + 1] = vector[x];
	}
	vector.pop_back();
	for (int i = 0; i < vector.size(); i++)
	{
		if (i < vector.size() - 1)
		{
			cout << vector[i] << ",";
		}
		else
		{
			cout << vector[i] << endl;
		}
	}
Dec 11, 2014 at 1:35am
Try using 'move'.

http://www.cplusplus.com/reference/algorithm/move/

move(vector.begin()'start', vector.begin()+'item location', vector.end()'new position');
Dec 11, 2014 at 4:14am
You can move the element you want to delete to the end of the vector (you can swap elements to do this) and use std::vector::pop_back, which will delete the last element in the vector (reducing the size of the container by 1).

http://www.cplusplus.com/reference/vector/vector/pop_back/
Last edited on Dec 11, 2014 at 4:15am
Dec 11, 2014 at 4:24am
I only want to shift the desired element to the end of the vector and delete it without moving the end element to where the first element was.
Can you elaborate on this? If you delete an element from the vector then what element should take its place?

Perhaps an example will help. Given a vector<int> containing 10 9 8 7, what should it contain after deleting 9?

If the answer is 10 8 7 then you can move the elements after 9 down one position and then resize the vector to shrink it by one element.
Dec 12, 2014 at 4:06am
This works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
 int num[] = {1, 2, 3, 4, 5};
 vector<int> numbers(num, num+5);

//-----------------------------------------------------------------------------

 cout << " Vector before moving" << endl;
 cout << "----------------------" << endl;

 for(int i=0; i<numbers.size(); i++) // Print vector
  {
   cout << numbers[i] << " ";
  }

 cout << endl << endl;

//-----------------------------------------------------------------------------

 cout << " Vector after moving" << endl;
 cout << "---------------------" << endl;
 
 numbers.push_back(numbers[2]);    // Add new '3' to back
 numbers.erase(numbers.begin()+2); // Remove original '3'

 for(int i=0; i<numbers.size(); i++) // Print vector
  {
   cout << numbers[i] << " ";
  } 
 
//-----------------------------------------------------------------------------
cin.ignore(); // Pause
cin.get();
return 0;
}
Topic archived. No new replies allowed.