
MongoDBDriverServer::getHost 함수가 반환하는 주소 형식에서 호스트 이름만 추출하는 방법은 다음과 같습니다.
1. 주소 형식에서 '://'를 포함한 부분을 제거합니다.
2. ':'를 포함한 부분을 제거합니다.
예제 코드는 다음과 같습니다.
#hostingforum.kr
cpp
#include
#include
std::string getHostName(const std::string& host) {
size_t start = host.find("://");
if (start != std::string::npos) {
host.erase(start, host.find('/', start) - start + 1);
}
start = host.find(":");
if (start != std::string::npos) {
host.erase(start, host.find('/', start) - start + 1);
}
return host;
}
int main() {
std::string host = "mongodb://localhost:27017";
std::cout << getHostName(host) << std::endl; // localhost
return 0;
}
위 코드에서 getHostName 함수는 주소 형식에서 호스트 이름만 추출하는 함수입니다. 이 함수는 '://'와 ':'를 포함한 부분을 제거하여 호스트 이름만 추출합니다. main 함수에서는 이 함수를 사용하여 호스트 이름을 추출하고 출력합니다.
2025-03-10 05:02