1

有人可以告诉我在 C++ 中使用 getline() 使用哪个标头。我用过

#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

他们似乎都没有工作。

这是我到目前为止编写的全部代码

#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;

class TextQuery
{
    typedef map<string, set<string::size_type> > word_map;
    TextQuery(ifstream &file)
    {
        FillLineVector(file);
        BuildMap(file);
    }

    void query(string);
private:
    word_map map;
    vector<string> LineVector;
    void FillLineVector(const ifstream&);
    void BuildMap(const ifstream&);
};

void TextQuery::FillLineVector(const ifstream& file)
{
    string line;
    while(getline(file,line)); //error:getline identifier not found
}
4

3 回答 3

2

您需要iostream并且可能string是您正在使用的版本。由于您没有提供其他信息,为什么这不起作用将不得不保持一个谜。

于 2012-05-09T19:03:06.450 回答
2

如果您希望人们能够帮助您,请发布您的代码。无论如何, getline 应该这样使用:

// getline with strings
#include <iostream>
#include <string>
using namespace std;

int main () {
  string str;
  cout << "Please enter full name: ";
  getline (cin,str);
  cout << "Thank you, " << str << ".\n";
}

http://www.cplusplus.com/reference/string/getline/所示

于 2012-05-09T19:04:08.947 回答
0

您的函数采用ifstream而不是istream,因此您必须包含<fstream>才能获得它的定义。

但是,您最好通过 take 使函数更通用istream,除非由于某种原因它们只能与文件一起使用。在这种情况下,你只需要你已经拥有的<iostream>and 。<string>

于 2012-05-09T20:28:50.403 回答