Why does my std::set find() not work? - c++

I have the following function and cannot figure out why it is not working.
Parameters are a set of Nonterminals and a vector of GrammarSymbol* (a word). Nonterminal is a subclass of GrammarSymbol. The function is supposed to filter all Nonterminals that are contained in the word as well as in the Nonterminal set and return them in a set.
std::set<Nonterminal> Filter(const std::set<Nonterminal>& symbolSet, const std::vector<GrammarSymbol*> w){
//resulting set
std::set<Nonterminal> rSet;
std::vector<GrammarSymbol*>::const_iterator wit;
std::set<Nonterminal>::const_iterator ntit;
//iterate over all symbols of the word
for(wit = w.begin(); wit != w.end(); wit++){
//test if current symbol is a nonterminal
const Nonterminal* nt = dynamic_cast<const Nonterminal*>(*wit);
if(nt != NULL){
std::cout << "current symbol " << nt->Str() << " is nonterminal" << std::endl;
for(ntit = symbolSet.begin(); ntit != symbolSet.end(); ntit++){
std::cout << ntit->Str() << " " << (!(*ntit < *nt) && !(*nt < *ntit))<< std::endl;
}
//look for the symbol in the nonterminal set
ntit = symbolSet.find(*nt);
//if the symbol was found, insert it into resulting set
if(ntit != symbolSet.end()){
rSet.insert(*ntit);
std::cout << "inserted " << ntit->Str() << "into set, size: " << rSet.size() << std::endl;
}
else{
std::cout << "not found in symbolSet" << std::endl;
}
}
}
return rSet;
}
This yields the output
current symbol (1, 2, 2) is nonterminal
(1, 2, 2) 1
(2, 3, 3) 0
(3, 2) 0
(4, 3) 0
(5, 3, 1) 0
not found in symbolSet
It works just fine if I don't rely on the filter function and filter on my own:
std::set<Nonterminal> Filter(const std::set<Nonterminal>& symbolSet, const std::vector<GrammarSymbol*> w){
//resulting set
std::set<Nonterminal> rSet;
std::vector<GrammarSymbol*>::const_iterator wit;
std::set<Nonterminal>::const_iterator ntit;
//iterate over all symbols of the word
for(wit = w.begin(); wit != w.end(); wit++){
//test if current symbol is a nonterminal
const Nonterminal* nt = dynamic_cast<const Nonterminal*>(*wit);
if(nt != NULL){
std::cout << "current symbol " << nt->Str() << " is nonterminal" << std::endl;
for(ntit = symbolSet.begin(); ntit != symbolSet.end(); ntit++){
std::cout << ntit->Str() << " " << (!(*ntit < *nt) && !(*nt < *ntit))<< std::endl;
if(!(*ntit < *nt) && !(*nt < *ntit)){
rSet.insert(*ntit);
}
}
}
}
return rSet;
}
Can anyone explain to me what happens here? As far as I know, std::set is supposed to compare elements with the operator<. Comparing seems to work just fine, as shown by the output.
I would just continue with the selfmade filter, but I fear there is a bigger underlying problem.
Thanks!
Edit: Nonterminal and operator< for Nonterminal:
class Nonterminal : public GrammarSymbol{
public:
/** The start state*/
Idx mStartState;
/** The stack symbol*/
Idx mOnStack;
/** The end state */
Idx mEndState;
//...
}
Idx is just a typedef for an int.
bool Nonterminal::operator<(const GrammarSymbol& other) const{
if(typeid(*this) != typeid(other)) return true; //other is a terminal
const Nonterminal& nt = dynamic_cast<const Nonterminal&>(other); //other is a nonterminal
if (mStartState < nt.StartState()) return true;
if (mOnStack < nt.OnStack()) return true;
if (mEndState < nt.EndState()) return true;
return false;
}

You operator < is incorrect
Consider
Nonterminal nt1 (1,2,3);
Nonterminal nt2 (3,2,1);
bool b1 = nt1 < nt2;
bool b2 = nt2 < nt1;
For nt1 < nt2 comparison:
1 < 3 immediatelly yelds true;
For nt2 < nt1:
3 < 1 doesn't hold, so you proceed to
2 < 2 which doesn't hold, so you proceed to
1 < 3 which holds
Thus both b1 and b2 will be true, which is nonsense
As for your second variant of filter, it works becase of logic error
for(ntit = symbolSet.begin(); ntit != symbolSet.end(); ntit++){
std::cout << ntit->Str() << " " << (!(*ntit < *nt) && !(*nt < *ntit))<< std::endl;
if(!(*ntit < *nt) && !(*nt < *ntit)){
rSet.insert(*ntit);
}
here rSet.insert(*ntit); will be called every time if(!(*ntit < *nt) && !(*nt < *ntit)) doesn't hold, not once as it should.

Related

Using Lambda Function with find_if C++20

I am getting a syntax error on the line
if (auto result = ranges::find_if(height.begi with a red squiggly line under find_if:
no instance of overloaded function matches the argument list
auto is_gt_or_eq = [height, i](int x) { height[x] >= height[i]; };
if (auto result = ranges::find_if(height.begin(), height.end(), is_gt_or_eq); result != height.end()) {
std::cout << "First larger or equal to element in height: " << *result << '\n';
}
else {
std::cout << "No larger or equal to element in height\n";
}
I found this similar code on cppreference, and it does run:
auto is_even = [](int x) { return x % 2 == 0; };
if (auto result = ranges::find_if(height.begin(), height.end(), is_even); result != height.end()) {
std::cout << "First even element in height: " << *result << '\n';
}
else {
std::cout << "No even elements in height\n";
}
I believe the error is in this line of code:
auto is_gt_or_eq = [height, i](int x) { height[x] >= height[i]; };
To start with, you forgot the return keyword for the return statement. So your lambda is returning void by default, thus not a valid predicate. The library won't allow you to call its function due to this mismatch.
Beyond that, x is (a copy of) the element of height, not an index of the container. There is no need to access the container again for the element in the lambda. So, the simplest fix is
auto is_gt_or_eq = [height, i](int x) { return x >= height[i]; };
There's also no need to constantly re-access height[i] in the lambda. It's not a bad idea to just capture that value instead.
auto is_gt_or_eq = [hi = height[i]](int x) { return x >= hi; };
Your lambda is now smaller, more inline-able and (to me at least) more readable.

Performing recursion on a list C++ [duplicate]

This question already has answers here:
How to remove from a map while iterating it?
(6 answers)
Closed 5 years ago.
I am trying to determine the maximum number of items I can remove from a list using std list to get the minimum size. However, it keeps ending up in bad memory access.
This is my recursive function:
int step (list<int> mylist) {
int count = mylist.size();
// Terminations
if (!checkRemaining(mylist)) {
return mylist.size();
}
if (mylist.empty()) {
return 0;
}
//printf("mysize: %d\n", mylist.size());
// Else we do not terminate first
for (auto i=mylist.begin(); i != prev(mylist.end()); ++i)
{
if ((*i + *next(i))%2 == 0) // Problem starts from here, bad access
{
mylist.erase(next(i));
mylist.erase(i);
printf("this size %lu\n", mylist.size());
list<int> tempList = mylist;
for (auto it = tempList.begin(); it != tempList.end(); it++) {
printf("%d ", *it);
}
printf("\n");
int temp = step (tempList);
if (temp < count) count = temp;
}
}
return count;
}
It managed to get down to the desired size but the program would crash due to bad memory access.
Once you do mylist.erase(i);, i is invalided, so your ++i in the loop is UB.
Your code should look like:
for (auto i = mylist.begin(); i != mylist.end() && i != prev(mylist.end()); /* Empty */)
{
if ((*i + *next(i)) % 2 == 0)
{
mylist.erase(next(i));
i = mylist.erase(i);
// maybe you want prev(i) if i != mylist.begin()
#ifdef DEBUG
std::cout << "this size " << mylist.size() << "\n";
for (const auto& e : myList) {
std::cout << e << " ";
}
std::cout << "\n";
#endif
count = std::min(count, step(myList));
} else {
++i;
}
}
In addition, final check should handle correctly when you remove last elements.

How to create a map with custom class/comparator as key

I have a class named ItemType. It has two members - both double, named m_t and m_f. Two items of type ItemType are considered to be equal if these two members differ from each other within respective tolerance levels. With this logic, the comparator function is so defined as well. However, when I insert objects of this type as key into a map, only one key is produced in the map, even though at least three such keys should be present:
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
class ItemKey
{
public:
ItemKey(double t, double f)
{
m_t = t;
m_f = f;
}
double m_t;
double m_f;
double m_tEpsilon = 3;
double m_fEpsilon = 0.1;
bool operator<(const ItemKey& itemKey) const
{
int s_cmp = (abs(itemKey.m_f - m_f) > m_fEpsilon);
if (s_cmp == 0)
{
return (abs(itemKey.m_t - m_t) > m_tEpsilon);
}
return s_cmp < 0;
}
};
int main()
{
// The pairs are the respective values of m_t and m_f.
vector<pair<double, double>> pairs;
// These two should belong in one bucket -> (109.9, 9.0), because m_f differs by 0.09 and m_t differs by just 1
pairs.emplace_back(109.9, 9.0);
pairs.emplace_back(110.9, 9.09);
// This one is separate from above two beause even though m_t is in range, m_f is beyong tolerance level
pairs.emplace_back(109.5, 10.0);
// Same for this as well, here both m_t and m_f are beyong tolerance of any of the two categories found above
pairs.emplace_back(119.9, 19.0);
// This one matches the second bucket - (109.5, 10.0)
pairs.emplace_back(109.9, 10.05);
// And this one too.
pairs.emplace_back(111.9, 9.87);
map<ItemKey, size_t> itemMap;
for (const auto& item: pairs)
{
ItemKey key(item.first, item.second);
auto iter = itemMap.find(key);
if (iter == itemMap.end())
{
itemMap[key] = 1;
}
else
{
itemMap[iter->first] = itemMap[iter->first] + 1;
}
}
// The map should have three keys - (109.9, 9.0) -> count 2, (109.5, 10.0) -> count 3 and (119.9, 19.0) -> count 1
cout << itemMap.size();
}
However, the map seems to have only 1 key. How do I make it work as expected?
Why isn't your version working?
You did well to create your own comparison function. To answer your question, you have an error in your operator<() function such that only returns true if m_f is outside of tolerance and m_t is within tolerance, which I'm guessing is not what you desired. Let's take a look.
int s_cmp = (abs(itemKey.m_f - m_f) > m_fEpsilon);
The above line basically is checking whether this->m_f and itemKey.m_f are within tolerance of eachother (meaning equal to each other). That is probably what was intended. Then you say
if (s_cmp == 0)
{
return (abs(itemKey.m_t - m_t) > m_tEpsilon);
}
If s_cmp is true, then it will have the value of 1, and it will have a value of 0 for false (meaning that they are not within tolerance of each other). Then you return true if the m_t value is within tolerance. Up to this point, you return true if m_f is not equal (according to tolerance) and if m_t is equal (according to tolerance). Then your last line of code
return s_cmp < 0;
will return true always since a boolean converted to an integer cannot ever be negative.
How to get it working?
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <vector>
struct ItemKey
{
double m_t;
double m_f;
static constexpr double t_eps = 3;
static constexpr double f_eps = 0.1;
ItemKey(double t, double f) : m_t(t), m_f(f) {}
bool operator<(const ItemKey& other) const
{
// Here it is assumed that f_eps and t_eps are positive
// We also ignore overflow, underflow, and NaN
// This is written for readability, and assumed the compiler will be
// able to optimize it.
auto fuzzy_less_than = [] (double a, double b, double eps) {
return a < b - eps;
};
bool f_is_less_than = fuzzy_less_than(this->m_f, other.m_f, f_eps);
bool f_is_greater_than = fuzzy_less_than(other.m_f, this->m_f, f_eps);
bool f_is_equal = !f_is_less_than && !f_is_greater_than;
bool t_is_less_than = fuzzy_less_than(this->m_t, other.m_t, t_eps);
return f_is_less_than || (f_is_equal && t_is_less_than);
}
};
int main()
{
using namespace std;
// The pairs are the respective values of m_t and m_f.
vector<pair<double, double>> pairs;
// These two should belong in one bucket
// -> (109.9, 9.0), because m_f differs by 0.09 and m_t differs by just 1
pairs.emplace_back(109.9, 9.0);
pairs.emplace_back(110.9, 9.09);
// This one is separate from above two beause even though m_t is in range,
// m_f is beyong tolerance level
pairs.emplace_back(109.5, 10.0);
// Same for this as well, here both m_t and m_f are beyong tolerance of any
// of the two categories found above
pairs.emplace_back(119.9, 19.0);
// This one matches the second bucket - (109.5, 10.0)
pairs.emplace_back(109.9, 10.05);
// And this one too.
pairs.emplace_back(111.9, 9.87);
map<ItemKey, size_t> itemMap;
for (const auto& item: pairs)
{
ItemKey key(item.first, item.second);
auto iter = itemMap.find(key);
if (iter == itemMap.end())
{
itemMap[key] = 1;
}
else
{
itemMap[iter->first] = itemMap[iter->first] + 1;
}
}
// The map should have three keys
// - (109.9, 9.0) -> count 2
// - (109.5, 10.0) -> count 3
// - (119.9, 19.0) -> count 1
cout << itemMap.size();
cout << "itemMap contents:" << endl;
for (auto& item : itemMap) {
cout << " (" << item.first << ", " << ")" << endl;
}
return 0;
}
There are a few things I changed above. I have a few suggestions also unrelated to the programming mistake:
Do not store boolean values into integer variables.
There's a reason that C++ introduced the bool type.
Write your code to be readable and in a way that the compiler
can easily optimize. You may notice I used a lambda expression
and multiple booleans. Smart compilers will inline the calls to
that lambda expression since it is only used within the local scope.
Also smart compilers can simplify boolean logic and make it
performant for me.
The m_tEpsilon and m_fEpsilon are probably not good to be
changable variables of the class. In fact, it may be bad if one
object has a different epsilon than another one. If that were the
case, which do you use when you do the < operator? For this
reason, I set them as static const variables in the class.
For constructors, it is better to initialize your variables in the
initializer list rather than in the body of the constructor. That
is unless you are doing dynamic resource allocation, then you would
want to do it in the constructor and make sure to clean it up if
you end up throwing an exception (preferrably using the RAII
pattern). I'm starting to get too far off topic :)
Even though class and struct are basically identical except for
the default protection level (class is private by default and
struct is public by default). It is convention to have it as a
struct if you want direct access to the member variables. Although,
in this case, I would probably set your class as immutable. To do
that, set the m_t and m_f as private variables and have a getter
m() and f(). It might be a bad idea to modify an ItemKey
instance in a map after it has been inserted.
Potential problems with this approach
One of the problems you have with your approach here is that it will be dependent on the order in which you add elements. Consider the following pairs to be added: (3.0, 10.0) (5.0, 10.0) (7.0, 10.0). If we add them in that order, we will get (3.0, 10.0) (7.0, 10.0), since (5.0, 10.0) was deemed to be equal to (3.0, 10.0). But what if we were to have inserted (5.0, 10.0) first, then the other two? Well then the list would only have one element, (5.0, 10.0), since bother of the others would be considered equal to this one.
Instead, I would like to suggest that you use std::multiset instead, of course this will depend on your application. Consider these tests:
void simple_test_map() {
std::map<ItemKey, size_t> counter1;
counter1[{3.0, 10.0}] += 1;
counter1[{5.0, 10.0}] += 1;
counter1[{7.0, 10.0}] += 1;
for (auto &itempair : counter1) {
std::cout << "simple_test_map()::counter1: ("
<< itempair.first.m_t << ", "
<< itempair.first.m_f << ") - "
<< itempair.second << "\n";
}
std::cout << std::endl;
std::map<ItemKey, size_t> counter2;
counter2[{5.0, 10.0}] += 1;
counter2[{3.0, 10.0}] += 1;
counter2[{7.0, 10.0}] += 1;
for (auto &itempair : counter2) {
std::cout << "simple_test_map()::counter2: ("
<< itempair.first.m_t << ", "
<< itempair.first.m_f << ") - "
<< itempair.second << "\n";
}
std::cout << std::endl;
}
This outputs:
simple_test_map()::counter1: (3, 10) - 2
simple_test_map()::counter1: (7, 10) - 1
simple_test_map()::counter2: (5, 10) - 3
And for the multiset variant:
void simple_test_multiset() {
std::multiset<ItemKey> counter1 {{3.0, 10.0}, {5.0, 10.0}, {7.0, 10.0}};
for (auto &item : counter1) {
std::cout << "simple_test_multiset()::counter1: ("
<< item.m_t << ", "
<< item.m_f << ")\n";
}
std::cout << std::endl;
std::multiset<ItemKey> counter2 {{5.0, 10.0}, {3.0, 10.0}, {7.0, 10.0}};
for (auto &item : counter2) {
std::cout << "simple_test_multiset()::counter2: ("
<< item.m_t << ", "
<< item.m_f << ")\n";
}
std::cout << std::endl;
std::cout << "simple_test_multiset()::counter2.size() = "
<< counter2.size() << std::endl;
for (auto &item : counter1) {
std::cout << "simple_test_multiset()::counter2.count({"
<< item.m_t << ", "
<< item.m_f << "}) = "
<< counter1.count(item) << std::endl;
}
std::cout << std::endl;
}
This outputs
simple_test_multiset()::counter1: (3, 10)
simple_test_multiset()::counter1: (5, 10)
simple_test_multiset()::counter1: (7, 10)
simple_test_multiset()::counter2: (5, 10)
simple_test_multiset()::counter2: (3, 10)
simple_test_multiset()::counter2: (7, 10)
simple_test_multiset()::counter2.count({3, 10}) = 2
simple_test_multiset()::counter2.count({5, 10}) = 3
simple_test_multiset()::counter2.count({7, 10}) = 2
simple_test_multiset()::counter2.size() = 3
Note that count() here returns the number of elements within the multiset that are considered equal to the ItemKey passed in. This may be advantageous for situations where you want to ask "how many of my points are within my tolerance of a new point?"
Good luck!

C++ list in list

I have two lists
list<int> s;
list<std::list<int>> q;
and I did the following assignment
q.push_front(s);
How can I display the contents of q since this
for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
for (s_iterator = q_iterator.begin(); s_iterator != q_iterator.end(); ++s_iterator)
cout << *s_iterator;
gives me an error?
You have to write the following way
for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
for (s_iterator = q_iterator->begin(); s_iterator != q_iterator->end(); ++s_iterator)
cout << *s_iterator;
or the following way
for (q_iterator = q.begin(); q_iterator != q.end(); ++q_iterator)
for (s_iterator = ( *q_iterator ).begin(); s_iterator != ( *q_iterator ).end(); ++s_iterator)
cout << *s_iterator;
provided that q_iterator and s_iterator are already declared. Otherwise you could write for example
for ( auto q_iterator = q.begin; /*...*/ )
Also you can use the range based for statement. For example
for ( const auto &s : q )
{
for ( int x : s ) std::cout << x << ' ';
std::cout << std::endl;
}
You probably know it, but I just want to make sure you remember about a space between angle braces.
std::list<std::list<int> >

STL Map doesn't erase

I have the following method in my program.
Weird thing is the data is not removed after I call erase.
Any idea?
map<int,obj>::iterator it = this->indexMap.find(id);
if(it != this->indexMap.end())
{
int mapSize = this->indexMap.size();
int dataSize = (*it).second.getDataMap().size();
//copy data to another node | even when it doesn't get into this if condition, it does not remove the data
if(mapSize> 1 && dataSize != 0)
{
it++;
this->copyData(id,it->first);
it--;
}
//remove peer | i've tried id and it, both does not work
this->indexMap.erase(it);
map<int,obj>::iterator iter = this->indexMap.find(id);
if(iter == this->indexMap.end())
{
cout << "ERROR" << endl;
}
}
Output:
ERROR
Thanks! :)
This block:
map<int,obj>::iterator iter = this->indexMap.find(id);
if(iter == this->indexMap.end())
{
cout << "ERROR" << endl;
}
prints out ERROR if an element with key id is not found in the map. Hence it has been removed.