我有一个包含超过 100 个 IP 地址的文件(点分十进制,例如 169.23.43.12)。现在我需要读取所有 IP 地址并按升序排序。为此,首先我尝试将所有IP地址转换为其等效的整数。我创建了一个C++函数来转换IP地址,但它不适用于大IP地址,例如255.250.120.100。我尝试使用 inet_aton() 和 inet_ntoa()。但使用这两个,我无法对 IP 地址进行排序。所以,请给我一个想法,将IP地址转换成可以排序的形式。下面是一些我尝试对 ip 地址进行排序的代码,但没有成功。
struct sockaddr_in antelope[2]; 字符 *some_addr;
inet_aton("60.0.0.4", &antelope[0].sin_addr); // store IP in antelope
inet_aton("10.0.0.2", &antelope[1].sin_addr); // store IP in antelope
std::sort(antelope,antelope+2);
cout<<inet_ntoa(antelope[0].sin_addr)<<endl;
cout<<inet_ntoa(antelope[1].sin_addr)<<endl;
Run Code Online (Sandbox Code Playgroud)