0

I'm trying to retrieve the value from the biggest key on my map, being the value a vector, and also trying to save that value into a new vector. The map keys are double, and values are vectors.

This is my map:

map <double, vector<long>> correlationValues1;

And I want to get the value with the highest key (Assuming map keys are sorted on ascending order):

vector<long> finalDirections1 = (correlationValues1.end())->second;

When I compile I get the error:

Error   C2664   'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)': el argumento 2 no puede convertirse de 'std::vector<double,std::allocator<_Ty>>' a 'const std::vector<long,std::allocator<_Ty>> &' TDI c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.12.25827\include\xmemory0   945 

Help anyone?

P.S: Sorry for my poor English.

P.P.S: Thanks for all the support guys!! It still delivers that compilation error... @the boy, I tried your method but still not working...

I don't know if this is actually influential, but when I place the keys and values into the map I use .emplace():

vector<double> arr1;
                arr1.push_back(temp1.FirstRow());
                arr1.push_back(temp1.LastRow());
                arr1.push_back(temp1.FirstCol());
                arr1.push_back(temp1.LastCol());
                vector<double> arr2;
                arr1.push_back(temp2.FirstRow());
                arr1.push_back(temp2.LastRow());
                arr1.push_back(temp2.FirstCol());
                arr1.push_back(temp2.LastCol());

                correlationValues1.emplace(correlation, arr1);
                correlationValues2.emplace(correlation, arr2);

I have no idea why it happens...

Angel
  • 19
  • 3
  • 3
    `correlationValues1.end()` doesn't point to the last element of your container. See: https://en.cppreference.com/w/cpp/container/map/end .You have to find the last one instead. – vahancho Apr 16 '19 at 12:55
  • 6
    First and foremost, end() doesn't return the last element of the container. Second, post a [mcve]. Your error message mentions `vector` which is not in the posted fragment. – n. m. could be an AI Apr 16 '19 at 12:58
  • 1
    https://stackoverflow.com/questions/289715/last-key-in-a-stdmap (spoiler: use `rbegin`). – p-a-o-l-o Apr 16 '19 at 13:03
  • Possible duplicate of [Last key in a std::map](https://stackoverflow.com/questions/289715/last-key-in-a-stdmap) – ShadowRanger Apr 16 '19 at 13:10
  • No, @ShadowRanger, this is about the misterious compilation error ... – p-a-o-l-o Apr 16 '19 at 13:11
  • 2
    It looks like your actual code has different types for the vectors. – molbdnilo Apr 16 '19 at 13:13
  • I tried this code and it is compiled, try to remove intermediate project files like .sdf file and recompile everything, it may be bug in VS – het_passagier Apr 16 '19 at 13:32

1 Answers1

2

firstly, map.end() is not valid iterator, the data of map is:

[ map.begin(), map.end() ). It means that iterator < end() and iterator >= begin().

if you want to access to the last element in map, you can use

map.rbegin()

or

--map.end()

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main()
{
    vector<long> v1 = {10,2,3};
    vector<long> v2 = {20,3,4};
    map <double, vector<long>> correlationValues1;
    correlationValues1.insert(pair<double, vector<long>>(10.0, v1));
    correlationValues1.insert(pair<double, vector<long>>(5.0, v2));

    vector<long> finalDirections1 = (correlationValues1.rbegin())->second;
}
the boy
  • 235
  • 1
  • 6