removing vector elements

I have a main vector container which will be passed vectors of different lengths
I would need to sum up all the first elements of the different vectors and delete the first element.

I am having difficulty removing the empty vector from my main container.
could you please show me how to do it thanks.

How do to remove the empty vector from my main container and free the memory.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int main()
{

vector< vector<int> > container;
vector<int> v1(2,1);	
vector<int> v2(4,2);	
vector<int> v3(6,3);

container.push_back(v1);
container.push_back(v2);
container.push_back(v3);
	
// just checking  sizes of each container
for (int i=0; i < container.size(); i++)
{
 cout << container[i].size() << endl;		
}
		
int x = 2;
int temp = 0;
int contain = container.size();

while(x > 1)
{
	for (int i=0; i < contain; i++)
	{		
          //need to add all first elements of the vector	
	  temp += container[i][0];
          
          //delete first element after reading
          if(container[i].size() > 1)
          {
    	     container[i].erase(container[i].begin());		
          }

	  // delete empty vector inside container.
          

          // new vectors can come in here 
          container.push_back(v1);
	  contain = container.size();

	  cout << temp << endl;	
          temp = 0;

         // if no more vectors are inserted stop program
	 if(container.size() == 0)
	 {
	   x = 0;
	   break;
	}
	
	cin >> x;
	return 0;	
}
closed account (Dy7SLyTq)
allocate it with new and destroy it with delete
in this case you should use container.size() instead of contain in the loop because after erasing an element of the vector its size will be changed.

1
2
3
4
5
6
for (int i=0; i < container.size(); i++)
{
   // ...
   if ( container[i].empty() ) container.erase( container.begin() + i );
   // ...
}

It looks like your algorithm for removing elements from the vectors contained in the "container" vector is wrong. you have

1
2
3
4
5
6
//delete first element after reading
          if(container[i].size() > 1)
          {
    	     container[i].erase(container[i].begin());		
          }


your statement only deletes the first element from vectors with size greater than one, if your vector only has one element in it then it will never be deleted. So your algorithm will never have empty vectors that need to be erased.
try
 
if(container[i].size() > 0)


after you correct that, you can delete an empty vector by searching for empty vectors in "container" and using the erase command to take them out.
(i thinK)
Last edited on
closed account (Dy7SLyTq)
wait what about a range based for loop or a for each loop?
well i think when you use erase, you have to assign the statement to a iterator;

like,

i=container[i].erase(container[i].begin());
thanks for the help.

I modified the code and is working fine.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <vector>
#include <set>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{

vector< vector<int> > container;
vector<int> v1(1,1);	
vector<int> v2(3,3);	
vector<int> v3(2,2);

container.push_back(v1);
container.push_back(v2);
container.push_back(v3);
	
cout << "container sizes" << endl;
// just checking  sizes of each container
for (int i=0; i < container.size(); i++)
{
 cout << container[i].size() << " ";		
}
cout << endl;

int x = 2;
int temp = 0;
int contain = container.size();

while(x > 1)
{
	for (int i=0; i < container.size(); i++)
	{	
		
		temp = temp + container[i][0];
		
		//delete first element after reading
		if(container[i].size() > 0)
		{
			//need to add all first elements of the vector	
			
			container[i].erase(container[i].begin());		
		}

		
		
	}	
	
	for (int i=0; i < container.size(); i++)
	{
		// delete empty vector inside container.
        if ( container[i].empty() ) container.erase( container.begin() + i );	
	}

	cout << temp << " ";
	temp = 0;

    // if no more vectors are inserted stop program
	if(container.empty())
	{
	   x = 0;
	   break;
	}
	
	cout << endl;
}
	cin >> x;
	return 0;	
}
Last edited on
Topic archived. No new replies allowed.