两个数之和等于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;
        }
    }
}

 

posted @ 2021-04-06 08:34  唯一诺  阅读(116)  评论(0)    收藏  举报