如果你有如下 URL:
snippet.cpp
std::string myURL = "https://subdomain.example.com/api/test";你可以使用 Boost::URL 从中解析主机名(你需要至少 Boost 1.81.0,之前的 boost 版本没有 Boost.URL)
snippet2.cpp
boost::urls::url_view url(myURL);
std::string host = url.host();
std::cout << host << std::endl; // 打印 "subdomain.example.com"
完整示例:
urlhost.cpp
#include <string>
#include <iostream>
#include <boost/url.hpp>
int main() {
std::string myURL = "https://subdomain.example.com/api/test";
boost::urls::url_view url(myURL);
std::string host = url.host();
std::cout << host << std::endl; // 打印 "subdomain.example.com"
}使用以下命令编译
build_urlhost.sh
g++ -o urlhost urlhost.cpp -lboost_url如果这篇文章对您有帮助,请考虑请我喝杯咖啡或通过 PayPal 捐款,以支持 TechOverflow 上新文章的研究与发布