Thread: A Question about maps and sets

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    A Question about maps and sets

    //I'm talking about the std::map and std::set here.

    Consider the following situation:

    I have a set defined as follows;
    Code:
    std::set<std::string> operators = {"+","-","*","/","^","&","=",":",".","%"};
    I also have a map declared as follows;
    Code:
    typedef token (*opfunc)(std::vector<token>);
    std::map<std::string,opfunc);
    Is there a way to *link* the contents of the former set to the later map, possibly with another container ? (I'd assign the function pointers by hand)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's wrong with

    map.insert("+", &your_function);

    ?

    Also, I'd make sure the function takes a reference to the vector for efficiency's sake.

    (Perhaps a good question would be: what are you trying to do?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    What's wrong with

    map.insert("+", &your_function);

    ?

    Also, I'd make sure the function takes a reference to the vector for efficiency's sake.

    (Perhaps a good question would be: what are you trying to do?)
    I do not have a problem with the insertion.

    What I'm trying to do :
    *Short Term : I have an Operator class which has a std::string member. I want to invoke the function assigned to the Operator from an object of the class. With the ( ) operator, or otherwise.
    But I also have a std::set of all permitted Operator strings.
    Is it possible to keep that a set and also launch the functions from the iterator returned by the find( ) algorithm ?

    *Long Term : Making a simple interpreter

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I still don't see why you don't have a map of allowed operations which are mapped to an appropriate function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    I still don't see why you don't have a map of allowed operations which are mapped to an appropriate function.
    I forgot that map has a find() too .

    btw.. Are those find `s similar in complexity?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, both have logarithmic lookup time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Maps Question
    By Scotty12 in forum C++ Programming
    Replies: 11
    Last Post: 08-20-2011, 10:49 AM
  2. Map of sets?
    By Orange Lozenge in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2011, 11:16 AM
  3. Question on Maps
    By dpp in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2009, 04:09 AM
  4. ATD: C++ Sets
    By stalker in forum C++ Programming
    Replies: 9
    Last Post: 11-09-2004, 06:40 AM
  5. disjoint sets conceptual question
    By axon in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2004, 10:49 PM