Vector of structure

Suppose we have a structure:

1
2
3
struct Position {
	unsigned int i;
	unsigned int j; };


and we have a vector: vector<Position> cp;

The aim of the program is that the vector<Position> cp gets values, such as i and j which are some coordinates. Then as the iteration goes on i and j get new values. What we needed is that the values of i and j be deleted before the new vales come, so that vector<Position> cp becomes an empty vector.

The question is how to do this.

I tried : cp.clear();
and I tried : cp.erase(cp.begin(),cp.begin()+2);
but it did not work.

Thanks in advance.
Last edited on
Well, cp.clear() would clear all of the Position objects out of the vector, not the position object's data members i and j.

If you're looking to simply clear the vector of it's position objects, cp.clear() will work fine, as shown in this sample program below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

int main()
{
	std::vector<int> myVector { 1, 2, 3, 4, 5};

	std::cout << "Before Clear: " << std::endl;
	for(auto x : myVector)
		std::cout << x << ", ";

	myVector.clear();

	std::cout << "\nAfter Clear: " << std::endl;
	for(auto x : myVector)
		std::cout << x << ", ";
}


Before Clear: 
1, 2, 3, 4, 5, 
After Clear: 


If you're looking to clear out the data members of the position objects in the vector, that would be done slightly differently.

Erase should also work, but you should use cp.erase(std::begin(cp), std::end(cp));

If cp.clear() is not working, it's more likely that your program is repopulating the vector erroneously, or there is some other logic error. If you supply contextual code to your question, I may be able to help more.
Last edited on
If, on the other hand, you'd like to delete the data members out of the position objects themselves, you'd have to iterate over each position object and call a clear method on each one to clear out their 'i' and 'j' values.
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
#include <iostream>
#include <vector>

struct Position
{
	int i;
	int j;

	void clear()
	{
		i = 0;
		j = 0;
	}
};

std::ostream& operator<<(std::ostream& io, Position& p)
{
	io << "(" << p.i << ", " << p.j << ")";
        return io;
}

int main()
{
	std::vector<Position> myVector {
		Position{1, 2},
		Position{4, 6},
		Position{2, 9}
	};

	std::cout << "Before Clear: " << std::endl;
	for(auto x : myVector)
		std::cout << x << std::endl;

	for(auto& x : myVector)
		x.clear();

	std::cout << "After Clear: " << std::endl;
	for(auto x : myVector)
		std::cout << x << std::endl;
}

Last edited on
Aaron Vienneau :
I want to delete the i and j values or as you mentioned the data members of the position objects in the vector. So, is there a way to do this? Otherwise, I have tried cp.clear(); and cp.erase(cp.begin(),cp.begin()+2); and that does not work as expected.

I also tried cp.erase(std::begin(cp), std::end(cp)); but it did not work.

gunnerfunner :
Thank you for the link you posted, however, cp.pop_back(); did not do what I want. It reacts as cp.clear(); .


Hi @Vahagn8,

Please see my second response above :)
Hi Aaron Vienneau :
By the time I posted I did not have seen your previous post :)
Thank you, I will look and write, in my country it is already night, I have to leave, so I will probably answer back tomorrow, as soon as I will look through the code.
Aaron Vienneau :
Thank you again for your help !
The method you provided works, so here is the sum up :

To delete the i and j values or the data members of the position objects in a vector consisting of e.g. structures we should iterate over each position object and call a clear method on each one. Below the key components of the code are presented :

1
2
3
4
5
6
7
8
9
10
11
struct Position
{
	unsigned int i;
	unsigned int j;

	void clear()
	{
		i = 0;
		j = 0;
	}
};


vector<Position> cp;

1
2
3
 for (auto& x : cp) {
	x.clear();
	} 


So, by doing this we delete the values of i and j by making them to be 0. However, this does not make vector<Position> cp; an empty vector.

On the other hand, if we want to delete the Position objects of the vector, we can simply use cp.clear(); . And this makes vector<Position> cp; an empty vector.

So, if we want to both delete the i and j values or the data members of the position objects in a vector and to transform the vector<Position> cp; into an empty vector we can use the two, one after another, which I did.
Last edited on
Hi Vahagn8,

What you've said sounds right, but you have a small error in your code segment.

1
2
3
4
5
6
7
8
9
10
11
struct Position
{
	unsigned int i;
	unsigned int j;

	void clear()
	{cp.clear(); // This is a scoping error. Position doesn't know about CP unless it's global.
		i = 0;
		j = 0;
	}
};


If you'd like to clear both the 'cp' vector and the 'i' and 'j' elemnents from the Positions, you'd do something like:

1
2
3
 for (auto& x : cp) { // This is saying, "For each Position in the vector 'cp'...
	x.clear(); // call 'clear()' on that position. Clear() as we've defined it, sets i and j to zero.
	} 


cp.clear() // Clears the vector of all of the position objects

I don't know if this will help, but think of 'std::vector<Position>cp' as a container of containers.



Last edited on
I edited. I do not know how it (cp.clear(); ) got in to the struct Position . Actually I did not used it in my original code. It happens sometimes. I mean I have noticed that recently my computer reacts in a strange manner, it copies and pastes things I do not have any clue. I think the problem is connected with the touch pad.
That is a good comparison. I was thinking of vector<Position> cp; like some object integrated into some other. Thanks for the help.
Last edited on
Topic archived. No new replies allowed.