Skip to content

Instantly share code, notes, and snippets.

@svagionitis
Created September 24, 2013 15:03
Show Gist options
  • Save svagionitis/6686101 to your computer and use it in GitHub Desktop.
Save svagionitis/6686101 to your computer and use it in GitHub Desktop.
Split/parse the query part of a URL. The correct one is getQueryParameters1.
#include <iostream>
#include <vector>
#include <utility>
std::vector< std::pair<std::string, std::string> > getQueryParameters(std::string query)
{
std::vector< std::pair <std::string, std::string> > queryParams;
size_t pos = 0;
std::string delimiter = "&";
std::string token;
while ((pos = query.find(delimiter)) != std::string::npos)
{
token = query.substr(0, pos);
queryParams.push_back(std::make_pair( token.substr(0, token.find_first_of("=")) ,
token.substr(token.find_first_of("=") + 1, token.length())));
std::cout << token.substr(0, token.find_first_of("=")) << " " << token.substr(token.find_first_of("=") + 1, token.length()) << std::endl;
query.erase(0, pos + delimiter.length());
}
return queryParams;
}
std::vector< std::pair<std::string, std::string> > getQueryParameters1(std::string query)
{
std::vector< std::pair <std::string, std::string> > queryParams;
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = query.find_first_of("&", current );
std::string token = query.substr(current, next - current);
queryParams.push_back(std::make_pair( token.substr(0, token.find_first_of("=")) ,
token.substr(token.find_first_of("=") + 1, token.length())));
std::cout << token.substr(0, token.find_first_of("=")) << " " << token.substr(token.find_first_of("=") + 1, token.length()) << std::endl;
}
while(next != std::string::npos);
return queryParams;
}
int main(int argc, char *argv[])
{
std::string s("freq=3939&band=8&serviceid=1101");
//getQueryParameters(s);
getQueryParameters1(s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment