2017-09-14 54 views
-4

如下stringstream的變量與未知分離

#include <sstream> 
#include <string> 
#include <iostream> 

std::string vars_to_string(std::string separator, double object_x, double object_y, double object_z, std::string msg) { 
    std::stringstream ss; 
    ss << msg << separator; 
    ss << object_x << separator; 
    ss << object_y << separator; 
    ss << object_z; 
    return ss.str(); 
} 

void string_to_vars(std::string text, std::string separator, double &object_x, double &object_y, double &object_z, std::string &msg) { 
    // ???????? 
} 

int main() { 
    double x = 4.2, y = 3.7, z = 851; 
    std::string msg = "position:"; 
    std::string text = vars_to_string("__%$#@!__", x, y, z, msg); 
    std::cout << text << std::endl; 

    double rx, ry, rz; 
    std::string rmsg; 
    string_to_vars(text, "__%$#@!__", rx, ry, rz, rmsg); 
    std::cout << "Retrived data: " << rx << ", " << ry << ", " << rz << ", " << rmsg << std::endl; 
    return 0; 
} 

現在我已經轉換了幾個數字的字符串從類型string一個分離器,我不知道我該怎麼做相反的結果變量轉換分成msg,object_x,object_y,object_z

若要停止建議微不足道的解決方案,我使用__%$#@!__作爲分隔符。分隔符由用戶決定,它可以是任意值,代碼不應該失敗。我不假設object_x是純數字。我只假定它不包含分隔符。

我們不知道分隔符。

有沒有簡單的解決方案,而不使用Boost?

問題是完全清楚的。如果有任何問題,請詢問,否則請重新打開問題

+2

'std :: getline()'允許指定_「未知分隔符」_。爲什麼你認爲有人恨你?你需要和心理學家聊天嗎? – user0042

+0

您可以通過向後推算來推斷分隔符。如果你知道字符串以結尾,那麼你可以在字符串上向後走,並得到分隔符字符串。然後使用它標記輸入字符串並將最後三個值轉換爲double。 – Rob

+0

@ user0042,非常感謝。我應該如何使用它?我在[wonderbox](https://wandbox.org/permlink/Ktbl19nnTLlh2Njm)中遇到錯誤。 – ar2015

回答

1

我可以給你推薦這段代碼嗎?

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> 

using namespace std; 

void dtokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& separator) 
{ 
    std::string::size_type pos = 0; 
    std::string::size_type lastPos = 0; 

    while(std::string::npos != pos) { 
     // find separator 
     pos = str.find(separator, lastPos); 

     // Found a token, add it to the vector. 
     tokens.push_back(str.substr(lastPos, pos - lastPos)); 

     // set lastpos 
     lastPos = pos + separator.size(); 
    } 
} 

std::string vars_to_string(std::string separator,double object_x, double object_y, double object_z, std::string msg) 
{ 
    std::stringstream ss; 
    ss<<msg<<separator; 
    ss<<object_x<<separator; 
    ss<<object_y<<separator; 
    ss<<object_z; 
    return ss.str(); 
} 

void string_to_vars(std::string text,std::string separator,double &object_x, double &object_y, double &object_z, std::string &msg) 
{ 
    vector<string> vars; 
    dtokenize(text, vars, separator); 

    if(vars.size() == 4) { 
     msg = vars[0]; 
     object_x = stod(vars[1]); 
     object_y = stod(vars[2]); 
     object_z = stod(vars[3]); 
    } 
} 

int main() 
{ 
    double x=4.2,y=3.7,z=851; 
    std::string msg="position:"; 
    std::string text=vars_to_string("__%$#@!__",x,y,z,msg); 
    std::cout<<text<<std::endl; 

    double rx,ry,rz; 
    std::string rmsg; 
    string_to_vars(text,"__%$#@!__",rx,ry,rz,rmsg); 
    std::cout<<"Retrived data: "<<rx<<", "<<ry<<", "<<rz<<", "<<rmsg<<std::endl; 

    return 0; 
}