开发者

Combine values of two maps based on keys

This is kind of last resort..

So I have two maps.

typedef std::map <string, vector<float> > Dict;
typedef std::map <string, string> Dict1;

The contents of the first map look like this: Dict = {A: -3.1, 2.1, 1.1}; {B: -4.5, 5.6, 7.2}...

Strings from the second map are same as keys from the first one. 开发者_运维技巧 Dict1 = {A: B};...

I need to create something like:

Dict2 = {-3.1, 2.1, 1.1:  -4.5, 5.6, 7.2}... 

or two place them in two vectors, but with possibility of reconstructing the structure of Dict1.. Technically these are coordinates for some points.

I actually went the second route and tried to create two vectors and then match them, but, apparently I made some mistake.. Here is what I have:

typedef std::map <string, vector<float> > Dict;
typedef std::map <string, string> Dict1;

typedef std::vector<float> V1;

V1 v1;
V1 v2;

Dict d;
Dict d1;


//Here is the code, I know, oh well...



for( map<string, vector<float> >::iterator iter0 = d.begin(); iter0 != d.end(); ++iter0 ) {

    for( map<string, string >::iterator iter1 = d1.begin(); iter1 != d1.end(); ++iter1 ) {

        vector <float> tempVal0 = (*iter0).second;
        string tempKey0 = (*iter0).first;

        string tempVal1 = (*iter1).second; 
        string tempKey1 = (*iter1).first;

        size_t comp1 = tempKey0.compare(tempKey1);
        if(comp1 == 0 ){
            for (unsigned i = 2; i < tempVal0.size(); i++) {
            v1.push_back(tempVal0[i-2]);
            v1.push_back(tempVal0[i-1]);
            v1.push_back(tempVal0[i]);

                for( map<string, vector<float> >::iterator iter00 = d.begin(); iter00 != d.end(); ++iter00 ) {

                    for( map<string, string >::iterator iter11 = d1.begin(); iter11 != d1.end(); ++iter11 ) {
                        vector <float> tempVal00 = (*iter00).second;
                        string tempKey00 = (*iter00).first;

                        string tempVal11 = (*iter11).second; 
                        string tempKey11 = (*iter11).first;

                        size_t comp2 = tempVal1.compare(tempKey00);
                        if (comp2 == 0){
                            for (unsigned i = 2; i < tempVal00.size(); i++) {
                                v2.push_back(tempVal00[i-2]);
                                v2.push_back(tempVal00[i-1]);
                                v2.push_back(tempVal00[i]);
                            }
                        }

                    }   
                    }     

            }
        }


    }
}

What am I missing??


std::map<string, vector<float>> d;
std::map<string, string> d1;
std::map<vector<float>, vector<float>> d2;

// Fill the maps here

for(std::map<string, string>::iterator i = d1.begin(); i != d1.end(); i++) {
    d2[d[i->first]] = d[i->second];
}

This is a fairly trivial operation with a basic working knowledge of the C++ Standard library. How you intend on comparing a vector of floats, I'm not wholly sure. C++ does not have a comparator for a vector of floats by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜