A Question about maps and sets

This is a discussion on A Question about maps and sets within the C++ Programming forums, part of the General Programming Boards category; //I'm talking about the std::map and std::set here. Consider the following situation: I have a set defined as follows; Code: ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490

    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)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 10: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, 05:40 AM
  5. disjoint sets conceptual question
    By axon in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2004, 09:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21