使用c++写一段代码解析出字符串中的client idserver endpoint和server id其中字符串格式为:clientid1server endpoint1server id1;clientid2server endpoint2server id2;
下面是使用C++编写的代码,可以解析出字符串中的client id,server endpoint和server id。
#include <iostream>
#include <vector>
#include <sstream>
struct ServerInfo {
std::string clientId;
std::string serverEndpoint;
std::string serverId;
};
std::vector<ServerInfo> parseServerInfo(const std::string& input) {
std::vector<ServerInfo> serverInfos;
std::stringstream ss(input);
std::string token;
while (std::getline(ss, token, ';')) {
std::stringstream ss2(token);
std::string clientInfo;
std::getline(ss2, clientInfo, ':');
std::string serverInfo;
std::getline(ss2, serverInfo, ':');
std::string clientId;
std::string serverEndpoint;
std::string serverId;
std::stringstream ss3(clientInfo);
std::getline(ss3, clientId, '@');
std::getline(ss3, serverEndpoint, '@');
std::stringstream ss4(serverInfo);
std::getline(ss4, serverEndpoint, '@');
std::getline(ss4, serverId, '@');
serverInfos.push_back({clientId, serverEndpoint, serverId});
}
return serverInfos;
}
int main() {
std::string input = "clientid1@server endpoint1::server id1;clientid2@server endpoint2::server id2;";
std::vector<ServerInfo> serverInfos = parseServerInfo(input);
for (const auto& serverInfo : serverInfos) {
std::cout << "Client ID: " << serverInfo.clientId << std::endl;
std::cout << "Server Endpoint: " << serverInfo.serverEndpoint << std::endl;
std::cout << "Server ID: " << serverInfo.serverId << std::endl;
std::cout << std::endl;
}
return 0;
}
这段代码会输出:
Client ID: clientid1
Server Endpoint: server endpoint1
Server ID: server id1
Client ID: clientid2
Server Endpoint: server endpoint2
Server ID: server id2
注意:此代码假设输入字符串的格式是正确的,并且每个字段之间用特定的分隔符进行分割。如果输入的字符串格式不正确,可能会导致解析错误
原文地址: https://www.cveoy.top/t/topic/hOcV 著作权归作者所有。请勿转载和采集!