简体   繁体   中英

qt pointer to qmap get value

I have a function that needs to access the value of a qmap pointer object.

void SomeClass::SomeFunction(QMap<qint64, bool>* times, qint64 startPoint, qint64 endPoint)
{
    //Here I want to check the value at an existing index.
}

What I tried:

times[key]; 

and:

&times[key];

and:

&times[key] == 0;

Both give a wrong result (the value is true but i am expecting false).

在此处输入图片说明

in qt the qmap has a method for that: contains

times->contains(startPoint)

so the way to go would be:

if (times->contains(startPoint))
{
    ....
}

update:

to get the value of the pointer you need to be aware of the operator precedence ie you have to do:

std::map<int, bool>*x = new std::map<int, bool>();
x->insert ( std::pair<int, bool>(0,false) );
x->insert ( std::pair<int, bool>(5,true) );
bool flag = (*x)[0];
std::cout << "at 0: " << flag  << std::endl;

flag = (*x)[5];
std::cout << "at 5: " << flag << std::endl;

so in your case

bool flag = (*times)[startPoint];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM