2013-09-11 54 views
0

這不是關於RapidXML約定的具體問題,因爲它是關於使用std :: vector的構造函數的問題。在std :: vector的構造函數中使用std :: string數據類型

在我發現別人用RapidXML的所有例子,大家總是將數據讀入的,像這樣的構造函數char「使用std::vector S」的矢量:

vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>()); 

必須有一個原因因爲當我嘗試將其更改爲std::string的一個載體,我得到滿屏幕的錯誤本是第一個錯誤:

error: invalid conversion from ‘void*’ to ‘std::istreambuf_iterator<std::basic_string<char> >::streambuf_type* {aka std::basic_streambuf<std::basic_string<char>, std::char_traits<std::basic_string<char> > >*}’ 

有沒有辦法使用的std :: string的方式,如果沒有,爲什麼?

回答

1

你想做什麼?

你想在單個字符串中的文件的內容?如果是,那麼

string buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>()); 

應該這樣做。

在另一方面,如果你想要一個字符串矢量,包含了文件中的單行每個字符串,你必須寫這樣的(未經測試的代碼)的循環:

vector <string> lines; 
for (string aLine; std::getline(theFile, aLine) ;) 
    lines.push_back(aLine); 
+0

哦, 我懂了! 'istreambuf_iterator()'逐字符讀取並且必須是'char'類型。我認爲如果我用'std:string'讀取項目,我就可以比較它們更容易。例如'if(column_node-> value()。compare(「Coil and Switch resistance」)== 0)'。 – larrylampco

+0

較短:'字符串緩衝區(istreambuf_iterator (theFile),{});' – sehe

相關問題