Vector find and erase struct

Hello,
i have a struct vector and a struct variable (without identifier).
I want to delete this struct variable from my struct vector.
This would be a example:
1
2
3
4
5
6
7
8
9
struct teststruct
{
	DWORD i;
	DWORD b;
}teststructvar;
//Give teststructvar some values...
std::vector<teststruct> teststructvector;
//Add teststructvector some variables...
teststructvector.erase(std::remove(teststructvector.begin(), teststructvector.end(), teststructvar), teststructvector.end());

I tried to make it like: http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector
but it seems when it is a struct, then it stop working.
(i did upgrade a old project up to v110, there it get ereased that way:
 
teststructvector.erease(teststructvar);
)
Last edited on
Do you have operator== defined for two teststructs?
No, the teststruct struct dont have operator defined.
Would i need to add something like?:
1
2
3
4
5
bool operator==(const teststruct& left, const teststruct& right)
{
    return left.i == right.i &&
           left.b == right.b;
}
Last edited on
No, the teststruct struct dont have operator defined.
Would i need to add something like?:


That would work, yes.
It wont work, it says no valid == operator found
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
#ifndef DWORD
#define DWORD unsigned long
#endif

#include <vector>
#include <algorithm>
using namespace std;

struct MyStruct
{
	DWORD i;
	DWORD b;

	friend bool operator==(const MyStruct& lhs, const MyStruct& rhs)
	{
		return lhs.i == rhs.i && lhs.b == rhs.b;
	}
};

int main()
{
	vector<MyStruct> v;
	// create some structs and add to vector:
	for (int n = 0; n < 100; n+=2)
	{
		MyStruct st = {n, n+1};
		v.push_back(st);
	}
	
	// now erase where teststruct::i == 10 and b == 11
	MyStruct temp = {10,11};
	v.erase(remove(v.begin(), v.end(), temp), v.end());
}
Last edited on
How do i set the == Operator when it is a typedef struct instead of a struct?

1
2
3
4
5
typedef struct
{
	DWORD i;
	DWORD b;
} MyStruct;


Besides, if i would change all "typedef structs" to "structs", would that be also a improvement of the code? (is it advised to use struct instead of typedef struct to the todays time)

Besides #2:
I dont even get the snippet of ajh32 compiled, it says, that the remove funktion dont accept 3 arguments.
Add #2:
Did need to add:
#include <algorithm>
Last edited on
Besides, if i would change all "typedef structs" to "structs", would that be also a improvement of the code? (is it advised to use struct instead of typedef struct to the todays time)


In C++, it isn't necessary to use the typedef. That's a C anachronism.
@ajh32: Line 13: Why friend?
@tcs - it's because the operator== is a binary operator whereas in the above struct I have declared two arguments to the operator==, under this scenario we need to declare the operator== as a friend because there is no '*this' outside the class instance. We need the operator== to have two operands otherwise it wouldn't play with the std::remove function that will pass two MyStructs through (in this example).

HTH
@ajh32 - There is no need to declare the operator a friend.

1
2
3
4
5
6
7
8
9
10
struct MyStruct
{
	DWORD i;
	DWORD b;
};

bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{
	return lhs.i == rhs.i && lhs.b == rhs.b;
}
Last edited on
@cire - well no not when it is external to the struct. I think it's cleaner to have the operator within the struct/class to which it refers to. But hey ho...
That seems a little disingenuous to me. It is external to the struct either way it is defined, and it is cleaner, IMO, for stuff which is external to the struct to be defined outside of the struct. It becomes even less attractive when the data is private and the operator can be implemented by using only the public interface.
Topic archived. No new replies allowed.