map::find

Maxim Vetrov muxas@mail.ru
Mon Oct 31 10:35:00 GMT 2005


Hi to all,

I'm stuck with map::find. Here is the description. I have a string with 
several parameters to be subtituted e.g.

SELECT #1,#2 FROM test;

where #1,#2 are parameters. To store the actual values of the parameters 
I use the map container where the key of the element is name of the 
parameter. In other words:

    class str_less: binary_function<char*,char*,bool>
    {
    public:
        result_type operator()(first_argument_type s1, 
second_argument_type s2) {
            return (result_type) strcmp(s1,s2)>0;
        };
    };

    class expr {
    private:
    ...
    map<char*,Param*,str_less> params;
    ...
    public:
    ...
    void addParam(ParamType t, void* v,char* ident="###")
        {
          Param *p = new Param(t,v,ident);
          params[ident]=p;
          return;
        }; end of addParam()
   
    Param *getParam(char* ident)
        {
            map<char*,Param*>::iterator p;
            p = params.find(ident);
            if (p==params.end()) {
                return NULL;
            } else {
                return p->second;
            };
        }; //end of get Param()
   
    int subst()
    {
        ...
        skipIrrelevant();
        getToken(); //get parameter name from a string
        while (position<len && token!=NULL) {
            Param* p = getParam(token);
            if (p!=NULL && p->type==PARAM_SQLEXPR) {
                
foundParams.insert(pair<int,Param*>(position-(p->ilength),p));
                newlen = newlen - p->ilength + p->vlength;
            } else {
        //            sdescription
        //            "Unknown parameter!"
                return 1;
            };
                skipIrrelevant();
                getToken();
            };
        ...
    }; //end of subst()
   
    }; //end of expr
   
Here is  how I use the above:
    ...
    expr *expr1 = new expr("SELECT #1,#2 FROM test;");
    char *value = "id";
    char *key = "#1";
    expr1->addParam(PARAM_SQLEXPR,value,key);
    value = "name";
    key = "#2";
    expr1->addParam(PARAM_SQLEXPR,value,key);
    expr1->subst();
    ...

When running subst function, it finds the first parameter in the 
container and never finds the second. Would be grateful for any help 
since I don't have any ideas.

Maxim



More information about the Libstdc++ mailing list