1

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)

Josh
  • 37
  • 6

1 Answers1

4

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.

chris
  • 60,560
  • 13
  • 143
  • 205
  • Thank you very much for the quick reply, I do have one more question, what is the purpose of the []? As It throws up an error in compilation – Josh Mar 14 '14 at 23:27
  • @Josh, It introduces the lambda, which is part of C++11, so you'll need to enable C++11 in your compiler (e.g., `-std=c++11`) or choose another option. – chris Mar 14 '14 at 23:28
  • My apologies, I was using an old makefile without the C++11 flag. I have implemented your code in, however it does not seem to be removing anything from the vectors. Also is there any way I can replace the 'i' with a string instead of an int? I have the individual elements assigned to a string called "std::string propagator;" Which i would be using to remove them from the vector. Thank you – Josh Mar 14 '14 at 23:50
  • @Josh, I've added a link to a live example in my answer showing that it works. As for your other question, I'm not sure what you're asking? What 'i', and what `int`? Could you perhaps give a better example of what you're trying to do? – chris Mar 14 '14 at 23:56
  • This helps greatly I thank you. Ah I meant 1. In this line "{return s.find('1')" Is there anyway I can replace it with"{return s.find(propagator)". As the string propagator changes throughout the runtime of the program. I hope this makes more sense, and again thank you – Josh Mar 15 '14 at 00:05
  • @Josh, Oh, sure, just subbing in `propogator` for `'1'` will make it use the current value of `propogator` as the searched for substring. – chris Mar 15 '14 at 00:06
  • Works perfectly! Thank you very much, I did however have to replace the [] with a [&] to get it working. Though I do need to look further into lambdas. You have been a great help, and I can not thank you enough! – Josh Mar 15 '14 at 00:21
  • @Josh, Oh, right, I didn't make that connection, sorry. You can't use the enclosing scope's variables unless they are captured. – chris Mar 15 '14 at 00:26