Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:37:43+00:00 2026-06-15T00:37:43+00:00

I have the following function and cannot figure out why it is not working.

  • 0

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;    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:37:44+00:00Added an answer on June 15, 2026 at 12:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in html, I cannot get the Function call back
I have the following function set up to sequentially fade in divs (with class
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the
I cannot figure out why g++ is giving the following errors. I believe that
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
I have the following function: func fitrange(a, x, b int) int { if a
I have the following function in a PHP script, which returns and API call:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.