其他分享
首页 > 其他分享> > 两个数之和等于target值

两个数之和等于target值

作者:互联网

void checkTarget()
{
    int target = 14;
    int buf[] = { 8, 2, 9, 10, 5, 4, 6 };
    std::map<int, int> tmpMap;
    for (int i = 0; i < sizeof(buf); i ++)
    {
        std::map<int, int>::iterator itor = tmpMap.find(target - buf[i]);
        if (itor == tmpMap.end())
        {
            tmpMap.insert(pair<int, int>(buf[i], i));
        }
        else
        {
            int index1 = itor->second;
            int index2 = i;
            cout << index1 << ":" << index2 << endl;
            break;
        }
    }
}

 

标签:map,两个,target,int,itor,tmpMap,等于,buf
来源: https://www.cnblogs.com/weiyouqing/p/14620165.html