我试图读取文本文件的每一行,每行包含一个单词并将这些单词放入向量中。我该怎么做?
这是我的新代码:我认为它仍有问题。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string line;
vector<string> DataArray;
vector<string> QueryArray;
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
cout<<QueryArray[0]<<endl;
cout<<DataArray[0]<<endl;
}
答案 0 :(得分:32)
std::vector<std::string> myLines;
std::copy(std::istream_iterator<std::string>(myfile),
std::istream_iterator<std::string>(),
std::back_inserter(myLines));
整个程序可能如下所示:
// Avoid "using namespace std;" at all costs. Prefer typing out "std::"
// in front of each identifier, but "using std::NAME" isn't (very) dangerous.
#include <iostream>
using std::cout;
using std::cin;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iterator>
using std::istream_iterator;
#include <algorithm>
using std::copy;
int main()
{
// Store the words from the two files into these two vectors
vector<string> DataArray;
vector<string> QueryArray;
// Create two input streams, opening the named files in the process.
// You only need to check for failure if you want to distinguish
// between "no file" and "empty file". In this example, the two
// situations are equivalent.
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
// std::copy(InputIt first, InputIt last, OutputIt out) copies all
// of the data in the range [first, last) to the output iterator "out"
// istream_iterator() is an input iterator that reads items from the
// named file stream
// back_inserter() returns an interator that performs "push_back"
// on the named vector.
copy(istream_iterator<string>(myfile),
istream_iterator<string>(),
back_inserter(DataArray));
copy(istream_iterator<string>(qfile),
istream_iterator<string>(),
back_inserter(QueryArray));
try {
// use ".at()" and catch the resulting exception if there is any
// chance that the index is bogus. Since we are reading external files,
// there is every chance that the index is bogus.
cout<<QueryArray.at(20)<<"\n";
cout<<DataArray.at(12)<<"\n";
} catch(...) {
// deal with error here. Maybe:
// the input file doesn't exist
// the ifstream creation failed for some other reason
// the string reads didn't work
cout << "Data Unavailable\n";
}
}
答案 1 :(得分:31)
最简单的形式:
std::string line;
std::vector<std::string> myLines;
while (std::getline(myfile, line))
{
myLines.push_back(line);
}
不需要疯狂的东西:)
编辑:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::string line;
std::vector<std::string> DataArray;
std::vector<std::string> QueryArray;
std::ifstream myfile("OHenry.txt");
std::ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
std::cout<<"Error opening output file"<< std::endl;
system("pause");
return -1;
}
while (std::getline(myfile, line))
{
DataArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
std::cout<<"Error opening output file"<<std::endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
std::cout<<QueryArray[20]<<std::endl;
std::cout<<DataArray[12]<<std::endl;
return 0;
}
关键字使用是非法的C ++!永远不要使用它。好吗?好。现在将我写的内容与你写的内容进行比较,并尝试找出差异。如果你还有问题,请回来。
答案 2 :(得分:19)
最简单的版本:
std::vector<std::string> lines;
for (std::string line; std::getline( ifs, line ); /**/ )
lines.push_back( line );
我省略了包含和其他垃圾。我的版本与FailedDev几乎相同,但是通过使用'for'循环,我将'line'的声明放在循环中。这不仅仅是减少行数的技巧。这样做会减少行的范围 - 它在for循环后消失。所有变量都应尽可能小,因此这样做会更好。对于循环很棒。
答案 3 :(得分:0)
C ++ 11及更高版本的简短版本。向量是根据文件内容直接构建的:
ifstream qfile("queries.txt");
vector<string> lines {
istream_iterator<string>(qfile),
istream_iterator<string>()
};