2014-01-27 119 views
0

我有幾個問題在下面。我只從Stackoverflow獲取了這段代碼並試圖理解。拆分和解析字符串,並在C++中轉換爲char *

如何分割代碼工作是具體的:我不明白下面的代碼

std::stringstream ss(s); 
std::string item; 
while (std::getline(ss, item, delim)) { 
    elems.push_back(item); 

我加入的代碼刪除空間,但它給我的編譯error.Again我已經採取代碼計算器並不能圖出了錯誤

split.cpp:34:88: error: no matching function for call to 
‘remove_if(std::vector<std::basic_string<char> >::iterator, 
std::vector<std::basic_string<char> >::iterator, <unresolved 
overloaded function type>)’ split.cpp:34:88: note: candidate is: In 
file included from 
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0, 
       from split.cpp:5: /linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5: 
note: template<class _FIter, class _Predicate> _FIter 
std::remove_if(_FIter, _FIter, _Predicate) 
/linux/depot/gcc-4.7.0/bin/../lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:1140:5: 
note: template argument deduction/substitution 
failed:split.cpp:34:88: note: couldn't deduce template parameter 
‘_Predicate’ 

我怎麼能轉換vector<string>爲char *

#include<iostream> 
    #include<string> 
    #include<sstream> 
    #include<vector> 
    #include<algorithm> 
    #include<cctype> 
    using namespace std; 

    std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { 
     std::stringstream ss(s); 
     std::string item; 
     while (std::getline(ss, item, delim)) { 
      elems.push_back(item); 
     } 
     return elems; 
    } 

    std::vector<std::string> split(const std::string &s, char delim) { 
     std::vector<std::string> elems; 
     split(s, delim, elems); 
     return elems; 
    } 

    int main() 
    { 
     std::vector<std::string> f_data; 
     f_data.push_back("A= 99.58%"); 
     f_data.push_back("B= 78%"); 
     f_data.push_back("C= 90%"); 
     vector<string>::iterator t_data; 
     for(t_data = f_data.begin(); t_data != f_data.end(); t_data++) 
     { 
      vector<string> temp_data = split(*t_data, '='); 
      //temp_data.erase(std::remove_if(temp_data.begin(), temp_data.end(), std::isspace), temp_data.end()); 
     vector<string>::iterator data; 
     for(data = temp_data.begin(); data != temp_data.end(); data++) 
     { 
      cout<<*data; 
     } 

    } 
    return 0; 
} 

回答

1

爲什麼你返回elems矢量?你通過引用傳遞它,你不必返回它。

void split(const std::string &s, char delim, std::vector<std::string> &elems) { 
    std::stringstream ss(s); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
    return elems; 
} 

當然,這看起來更好(注意,它是由返回值):

std::vector<std::string> split(const std::string &s, char delim) { 
    std::vector<std::string> elems; 
    std::stringstream ss(s); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
    return elems; 
} 

至於如何轉換vector<string>char*,串聯在載體中的字符串,然後訪問內部緩衝區產生的字符串與c_str()

0

有兩個std :: isspace(一個在< cctype>,另一個在<區域設置>)。你也應該刪除空格,而不是向量中的字符串:

inline bool is_space(char c) { return std::isspace(c); } 


... 
for(std::vector<std::string>::iterator t = temp_data.begin(); t != temp_data.end(); ++t) { 
    std::string& s = *t; 
    std::remove_if(s.begin(), s.end(), is_space); 
} 
... 
+0

hmm。得到它了。我犯了這個錯誤 – user765443