Help implementing std::remove_if?

I'm trying to iterate through a vector and delete elements in it. I've seen that std::remove_if is probably the best way to go about doing this, but I need help with the Predicate argument of the function. Here is my code + explanation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Level::RemoveLine(int line){
	std::cout << "removing line " + line;
	levelRects_.erase(std::remove_if(levelRects_.begin(), levelRects_.end(), TestForLine(line) == true), levelRects_.end());
}

bool Level::TestForLine(int line){
	for(int i = 0; i < gamePieces_.size(); i++){
		//test for each brick's line being equal to line, return true
		for(int j = 0; j < 4; j++){
			if(gamePieces_[i].GetPieceRectangles()[j].GetActiveLine() == line){
				return true;
			}
			else{
				return false;
			}
		}
	}
}

TestForLine() is supposed to be the predicate for std::remove_if, but I don't know exactly how to use it as a predicate. This code throws a compiler error "C2064 term does not evaulate a function taking one arguments". So I want to erase objects from a vector if their function TestForLine() returns true; how do I put that into terms for std::remove_if?
It looks like you won't be able to use remove_if here.

Predicate is a function/functor which takes one argument arnd returns true of false depending on it.
Unless your levelRects_ contains int, you cannot use TestForLine as predicate.

And due to how it is written, you can simplify your TestForLine:
1
2
3
bool Level::TestForLine(int line){
    return gamePieces_[0].GetPieceRectangles()[0].GetActiveLine() == line
}
The thid parameter is a function (like). See:

http://www.cplusplus.com/reference/algorithm/remove_if/?kw=remove_if
levelRects_ contains class objects. So if I can't use std::remove_if, what can I use to erase elements from the vector?
what can I use to erase elements from the vector?
You can use a remove_if. You jkust need a funtion which takes a reference to object and decided if it needs to be removed. TestForLine is not suits this for obvious reasons (actually it kinda pointless)
You may use lambda:

http://www.cprogramming.com/c++11/c++11-lambda-closures.html

like so:
1
2
3
levelRects_.erase(std::remove_if(levelRects_.begin(), levelRects_.end(), 
[&] (const levelRect& lr) { return lr.GetLine() == line; }
), levelRects_.end());
So I need a function (let's keep the name TestForLine) that takes a reference to the class object inside the vector, like so?
bool Level::TestForLine(Brick &brick)
Also, my problem is that I need to pass line down to TestForLine(). The compiler won't let me include line as a parameter but how else can the Brick objects check if their line is equal to the line parameter?

@coder777 that seems like it would work, but lr.GetLine() throws this error:
1
2
	5	IntelliSense: the object has type qualifiers that are not compatible with the member function
            object type is: const Brick	c:\Users\jordan\Documents\Visual Studio 2012\Projects\Tetris\Tetris\Level.cpp	55" 

edit: removed the const declaration and it seems to work now. Will try to compile the full program when I get back
Last edited on
Odd, now that I removed const in the code above, my console output gives a "!" each time the function is called.
instead of removing const add const to the GetLine() (or GetActiveLine()) function.

But I'd think that this is not the problem. Is it possible that you have dangling references?
Topic archived. No new replies allowed.