andre ahmed -4 years ago 76
C++ Question

# Concatenting string what's the best way for doing that?

I have a query string that is formed in a std::string, the query has 4 points and each point has an x,y, each them of is of double datatype.

How would I concatenate the string to put the 4 points (8 variables) inside that query in c++

``````'MULTIPOINT(16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549)';
``````

here is the form of the query:

``````string query = "SELECT * from areas WHERE st_contains(ST_ConvexHull(areas.GEOMETRY),st_mpointfromtext('MULTIPOINT(16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549)',4326));";

double x1 = 6.17951, x2 = 6.17951, x3 = 6.17951, x4 = 6.17951;
double y1 = 7.85549, y2 = 7.85549, y3 = 7.85549, y4 = 7.85549;

/* std::string query = "SELECT * from areas WHERE st_contains(ST_ConvexHull(areas.GEOMETRY),st_mpointfromtext('MULTIPOINT("
+ to_string(x1) + " " + to_string(y1) + ", "
+ to_string(x2) + " " + to_string(y2) + ", "
+ to_string(x3) + " " + to_string(y3) + ", "
+ to_string(x4) + " " + to_string(y4) + "',4326));";
*/

string query = "SELECT * from areas WHERE st_contains(ST_ConvexHull(areas.GEOMETRY),st_mpointfromtext('MULTIPOINT(6.17951 7.85549, 6.17951 7.85549, 6.17951 7.85549, 6.17951 7.85549)',4326));";
``````

Depending on your input type there are a few options.

If you have a `std::string` then do like this:

``````std::string mp = "'MULTIPOINT(16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549, 16.17951 47.85549)';";
std::string query = "SELECT * from areas WHERE st_contains(ST_ConvexHull(areas.GEOMETRY),st_mpointfromtext(" + mp + ",4326));";
``````

If your input is `doubles`, and you use `C++11`, then you could do it like this:

``````std::string query = "SELECT * from areas WHERE st_contains(ST_ConvexHull(areas.GEOMETRY),st_mpointfromtext('MULTIPOINT("
+ std::to_string(x1) + " " + std::to_string(y1) + ", "
+ std::to_string(x2) + " " + std::to_string(y2) + ", "
+ std::to_string(x3) + " " + std::to_string(y3) + ", "
+ std::to_string(x4) + " " + std::to_string(y4) + "',4326));";
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download