I would like to store data in an object array but I don't know how to split my string.
The result I would like to see is:
tab[0].username = "user1"
tab[0].ip = "192.168.0.1"
tab[1].username = "user2"
tab[1].ip = "192.168.0.2"
tab[2].username = "user3"
tab[2].ip = "192.168.0.3"
Here's how my string looks:
user1:192.168.0.1|user2:192.168.0.2|user3:192.168.0.3
The code I currently have, which only allows you to split without managing the pipe :
void addInTab(std::vector<std::string> vec, client *tab, int total_user)
{
for(int i = 0; i < 2; ++i) {
if (i == 0)
tab[total_user].username = vec[i];
if (i == 1)
tab[total_user].ip = vec[i];
}
}
void split(std::string str, char delim)
{
std::vector<std::string> vec;
std::string::size_type tmp = str.find(delim);
while(tmp != std::string::npos) {
vec.push_back(str.substr(0, tmp));
str = str.substr(tmp + 1);
tmp = str.find(delim);
}
vec.push_back(str);
addInTab(vec);
}
thank advance