1

I have a map of pairs map<pair<int, int>, int> counts. I am iterating through this and I am calculating some float numbers, if the value of the map is bigger than 2. Now I want to save the float numbers in a vector that is as big as the number of similar values in my first element of my pair. An example of values in my map:

1 1 4
1 2 5
1 3 5
1 7 5
1 29 2 .. now the first key changes so the size would be 4 (not 5, because one value is < 3)
2 10 1
2 20 4 ...

I tried to make a minimal example, which explains it a little bit better:

int main(int argc, char** argv)
{
    // Create some values - both vectors have the same size
    vector<pair<int, int>> pairs;
    pairs.push_back(make_pair(1, 1));
    pairs.push_back(make_pair(1, 2));
    pairs.push_back(make_pair(1, 4));
    pairs.push_back(make_pair(2, 7));
    pairs.push_back(make_pair(2, 4));
    pairs.push_back(make_pair(3, 5));
    pairs.push_back(make_pair(3, 7));
    pairs.push_back(make_pair(3, 8));

    vector<float> value;
    value.push_back(4);
    value.push_back(5);
    value.push_back(8);
    value.push_back(2);
    value.push_back(5);
    value.push_back(6);
    value.push_back(7);
    value.push_back(8);

    map<pair<int, int>, int> counts;
    vector<vector<vector<float>>> descriptors_1, descriptors_2;
    vector<float> oa_simil;
    float Overall_Similarity;

    for (size_t i = 0; i < pairs.size(); i++) {
        counts.insert(make_pair(make_pair(pairs[i].first, pairs[i].second), value[i]));
    }

    for (const auto& p : counts) {
        const auto& p1 = p.first.first;
        const auto& p2 = p.first.second;
        int count = p.second;
        if (p.second >= 3) {
            float S = 0;
            // Two for-loops that calculate a new S for every p1, p2 combination >= 3
            //S = ls_1;
            // 5 Linesegments in image 1 are compared to 5 Linesegments in image 2, if p.second >= 3
            for (int ls_1 = 0; ls_1 < 5; ls_1++) {  
                for (int ls_2 = 0; ls_2 < 5; ls_2++) {
                    pair<int, int> index_1, index_2;
                    index_1 = make_pair(p1, ls_1);
                    index_2 = make_pair(p2, ls_2);

                    // Calculate the Similarity of different pairs of line segments in a complex function
                    calculateSimilarity(Overall_Similarity, descriptors_1, descriptors_2, index_1, index_2);
                    oa_simil.push_back(Overall_Similarity);
                }
                // Get the maximum similarity of every single line segment in one image
                float max_M = *max_element(oa_simil.begin(), oa_simil.end());
                oa_simil.clear();

                // Sum up the maxima -> S contains the maximum for the combination of two Linesegments p1, p2 (if p >=3)
                S += max_M;
            }

            // Now I want to get the maximum value S for every single p1
            // In the example I want the maximum S out of e.g. the 3 pairs (1,1), (1,2), (1,4).
            // Trying to push the S in a vector<float> when p1 changes.

        }

    }

}

I already tried to get all my keys and the value of S into a new map but that seems like a waste of computation time when I am already iterating through my keys.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Grillteller
  • 891
  • 10
  • 26
  • Why not change your map to `map, pair>` and store the calculated value in the map itself. – Jonathan Potter Apr 25 '18 at 12:36
  • In the next step I want to calculate the largest of these float numbers for every **first** key. – Grillteller Apr 25 '18 at 12:41
  • @Grillteller what exactly you want to see as the output? can you say more about your second loop( you have said that "Two for-loops that calculate a new S for every p1, p2 combination >= 3"? what is it for second loop? and are you trying to omit the insertion of keys and values where values < 3? – JeJo Apr 25 '18 at 17:32
  • I tried to make the code more understandable and added the for loops. – Grillteller Apr 26 '18 at 07:51
  • @Grillteller The question is still not so clear. you want to get the maximum value S for every single p1, am I right? and then what? or do you have any other difficulties? is Overall_Similarity = S? and what is oa_simil? is it a vector; if so where it declared? – JeJo Apr 26 '18 at 21:13
  • Yes, the maximum value S for every single p1. That's it! As already declared (above the for-loops) oa_simil is a vector of float and my Overall_Similarity is a float. S is the maximum element in this vector that I get inside my for-loops. – Grillteller Apr 30 '18 at 06:54

1 Answers1

0

I think I solved it. I created a map for my pairs and S values and inversed the result to make it possible looking for the biggest value. Then I compared my p1 with the p1 of the previous iteration to test whether this value changed.

template<typename A, typename B>
std::pair<B, A> flip_pair(const std::pair<A, B> &p)
{
    return std::pair<B, A>(p.second, p.first);
}

template<typename A, typename B>
std::multimap<B, A> flip_map(const std::map<A, B> &src)
{
    std::multimap<B, A> dst;
    std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
        flip_pair<A, B>);
    return dst;
}

int main(int argc, char** argv) 
{

map<pair<int, int>, float> similarities;
multimap<float, pair<int, int>> inversed_map;

...             

               // Get the maximum similarity of every single line segment in one image
                float max_M = *max_element(oa_simil.begin(), oa_simil.end());
                oa_simil.clear();
                // Sum up the maxima -> S contains the maximum for the combination of two Linesegments p1, p2 (if p >=3)
                S += max_M;
            }


            similarities.insert(make_pair(make_pair(p1, p2), S));
            inversed_map = flip_map(similarities);

            if ((p1 > p1_old) && ((int)inversed_map.size() > 0)) {
                // Sort the vector and keep track of the indexes of the pair.
                auto it = inversed_map.rbegin();
                cout << it->second.first << " " << it->second.second << " " << it->first << endl;

                similarities.clear();
                inversed_map.clear();

            }
            p1_old = p1;


        }

    }

}
Grillteller
  • 891
  • 10
  • 26