编辑代码

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

unsigned int ipv4StringtoValue(string ipString) {
    unsigned int ipValue = 0;
    size_t dotPos = string::npos;
    size_t startPos = 0;

    while ((dotPos = ipString.find(".", startPos)) != string::npos)
    {
        string segment = ipString.substr(startPos, dotPos - startPos);
        unsigned int segmentValue = atoi(segment.c_str());
        ipValue <<= 8;
        ipValue += segmentValue;
        startPos = dotPos + 1;
    }

    if(startPos < ipString.length()) {
        string segment = ipString.substr(startPos);
        unsigned int segmentValue = atoi(segment.c_str());
        ipValue <<= 8;
        ipValue += segmentValue;
    }
    return ipValue;
}

struct IPLocation {
        string ipStart;
        string ipEnd;
        string location;

        IPLocation(string ipStringStart, string ipStringEnd, string locationValue) {
            ipStart = ipStringStart;
            ipEnd = ipStringEnd;
            location = locationValue;
        }
};



bool compareSmallerIpEnd(IPLocation a, IPLocation b) {
    return ipv4StringtoValue(a.ipEnd) < ipv4StringtoValue(b.ipEnd);
}

int binarySearchFirstLargerEqual(string searchIp, IPLocation *ipLocations, int locationLen) {
   int low = 0;
   int high = locationLen - 1;
   int keyPos = -1;

   int searchIpValue = ipv4StringtoValue(searchIp);

     while (low <= high) {
        int mid = low +((high - low+1) >> 1);
        if (ipv4StringtoValue(ipLocations[mid].ipEnd) >= searchIpValue) {
            if (mid == 0 || ipv4StringtoValue(ipLocations[mid - 1].ipEnd) < searchIpValue) {
                if (ipv4StringtoValue(ipLocations[mid].ipStart) <= searchIpValue){
                    keyPos = mid;
                }   
                break;
            }
            else {
                high = mid - 1;
            }
        }
        else {
            low = mid + 1;
        }
    }

   return keyPos;
}

void findIpLocation(string ip, IPLocation *ipLocations, int locationLen) {
    //按照从小到大顺序对ipLocations进行排序
    sort(ipLocations, ipLocations + locationLen , compareSmallerIpEnd);

    int ipPos = binarySearchFirstLargerEqual(ip, ipLocations, locationLen);

    if (ipPos != -1) {
        cout << ip << " is in " << ipLocations[ipPos].location << endl;
    }
    else {
        cout << "Fail to find location of " << ip << endl;
    }
    
}

int main(){
    IPLocation ipLocations[] = {
        IPLocation("202.102.133.0", "202.102.133.255", "Shandong Dongying"),
        IPLocation("202.102.135.0", "202.102.136.255", "Shandong Yantai"),
        IPLocation("202.102.156.34", "202.102.157.255", "Shandong Qingdao"),
        IPLocation("202.102.48.0", "202.102.48.255", "Jiangsu Suqian"),
        IPLocation("202.102.49.15", "202.102.51.251", "Jiangsu Taizhou"),
        IPLocation("202.102.56.0", "202.102.56.255", "Jiangsu Lianyungang"),
    };

    string ip = "";
    while (cin >> ip && ip != "exit"){
        findIpLocation(ip, ipLocations, sizeof(ipLocations)/sizeof(IPLocation));
    }


}