2017-03-23 37 views
1

我有一個查詢字符串形成一個std :: string,查詢有4個點,每個點有一個x,y,他們每個是雙數據類型。Concatening字符串這樣做的最佳方式是什麼?

我將如何將字符串連接到放4個點(8個變量)查詢中使用C++

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

這裏是查詢的形式:

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));"; 
+0

聽起來像你」重新尋找['std :: stringstream'](http://en.cppreference.com/w/cpp/io/basic_stringstream)? –

回答

1

根據您輸入鍵入有幾個選項。

如果你有一個std::string那麼這樣做:

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));"; 

如果您輸入doubles,並使用C++11,那麼你可以做這樣的:

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));"; 
+0

我已經使用to_string執行了你的查詢,但是它確實返回了查詢的全部結果,但是如果我編輯過的文章中顯示的那樣硬編碼了這些點,它將根據輸出在邏輯上進行正確的查詢。 – andre

+0

當這樣的事情不起作用時,只需比較兩個查詢字符串即可。有什麼不同? – Jonas

+0

完全沒有區別,那很奇怪。 – andre

相關問題