2016-04-28 27 views
0

我想要一個矢量文檔變量,將看起來像轉換一個文本文件導入的std ::矢量<string>

document[0] = "I" 
document[1] = " " 
document[2] = "want" 
document[3] = " " 
document[4] = "cake" 
document[5] = "." 
document[6] = "\n" 

隨着文件在給定行「我要的蛋糕。\ n」個

我我不知道如何去做這件事,而我在分隔符上找到的所有東西都會擺脫空白或其他東西。

我有一個unordered_set的停用詞,我想從文件中刪除。我設置的方法將迭代一個向量,並且remove_if單詞在我的停用詞中。

目標是將文檔向量中的所有元素放入一個沒有停用詞的新文件中。

std::vector<string> MakeFileVector(string filename){ 
//Get the input from the file 
std::ifstream input(filename.c_str()); 
std::vector<string> doc; 
string line; 

//For each line in the text File 
for (line ; getline(input, line);) 
{ 
    //somehow split up each word/space/period/comma/newline char 
    //and add to the doc vector 
    //for each word/space/period/comma/newline char 
    doc.push_back(str) 
} 
return doc; 
} 
+0

添加了一些更多解釋 – HunterH

+1

如果'I'和'want'之間有2個空格,是否應該是'「I」,「」,「」,「想要」或什麼 –

+0

不應該爲我的目的,它可以是「」,在一個字符串內有兩個空格 – HunterH

回答

0

您可以使用的std :: noskipws發現here。這將確保從流中讀取時不會跳過空格。或者,您也可以使用std :: getline,找到here將行加入std :: string中,然後處理空白。

1
#include <algorithm> 
#include <iterator> 
#include <vector> 
#include <string>  

ifstream myfile("textline.txt");  

    std::vector<std::string> myLines; 
     std::copy(std::istream_iterator<std::string>(myfile), 
        std::istream_iterator<std::string>(), 
        std::back_inserter(myLines)); 

你在這裏!

+0

這很接近,但它仍然存儲諸如「速率」之類的內容。我想要一些能夠在一個索引中存儲「速率」的東西,它是「。」在另一個 – HunterH

相關問題