Frage

I am implementing the unit clause propagation algorithm into c++. I have read in the CNF file to a vector with each clause in individual element of a vector, so for example

1 2 0
1 2 3 0
1 0
3 4 0

So far I am able to isolate individual elements and set them as a string, so in this example i would set the string to be "1".

The next step would be to remove all elements in the vector which contain a 1, so in this example the 1st, 2nd and 3rd elements would be removed. However when i run the vector remove command

clauses.erase(std::remove(clauses.begin(), clauses.end(), "1"), clauses.end());

It will only remove elements which are exactly "1", not the elements which contain a 1 as well as other characters. Is there anyway to remove any element of a vector which contains the string?

(I hope this makes sense, thank you for your time)

War es hilfreich?

Lösung

Use std::remove_if and search for a 1 in the string (live example):

clauses.erase(
    std::remove_if(clauses.begin(), clauses.end(), 
        [](const std::string &s) {return s.find('1') != std::string::npos;} 
    ), 
    clauses.end()
);

If you don't have C++11 for the lambda, a normal function, or functor, or Boost lambda, or whatever floats your boat, will work as well.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top