简体   繁体   English

使用流迭代器初始化向量

[英]Initializing a vector with stream iterators

I'm trying to initialize a vector using iterators and I'm getting a compiler error basically saying that there's no matching function to call. 我正在尝试使用迭代器初始化向量,并且出现编译器错误,基本上是说没有要调用的匹配函数。

The code reads from a file with an istream_iterator and ends with an input sentinel. 该代码使用istream_iterator从文件中读取,并以输入前哨结束。 Then I try to initialize the vector with those two iterators. 然后,我尝试使用这两个迭代器初始化向量。

#include "std_lib_facilities.h"
#include<iterator>

int main()
{
    string from, to;    // get source and target file names
    cin >> from >> to;

    ifstream is(from.c_str()); // open input stream
    ofstream os(to.c_str());   // open output stream

    istream_iterator<string> ii(is);    // make input iterator for stream
    istream_iterator<string> eos;   // input sentinel
    ostream_iterator<string> oo(os,"\n");

    vector<string> words(ii, eos);  // vector initialized from input
    sort(words.begin(), words.end());   // sort the buffer
    copy(words.begin(), words.end(), oo);  // copy buffer to output
}

I know I could use the copy function to copy the input stream into the vector, but I read that it can be done this way as well. 我知道我可以使用复制功能将输入流复制到向量中,但是我读到它也可以通过这种方式完成。 Can anyone explain why this is not compiling? 谁能解释为什么不编译? Thanks. 谢谢。

Compiler error: 编译器错误:

C:\Users\Alex\C++\stream_iterators.cpp|16|error: no matching function for call to `Vector<String>::Vector(std::istream_iterator<String, char, std::char_traits<char>, ptrdiff_t>&, std::istream_iterator<String, char, std::char_traits<char>, ptrdiff_t>&)'|

Edit: It is not a header problem. 编辑:这不是标题问题。 Std_lib_facilities has all of the needed headers. Std_lib_facilities具有所有必需的标头。

vector<string> words(ii, eos);

is an analogue to 是类似于

vector<string> words;
copy( ii, eos, back_inserter(words) );

vector class has the following constructor: vector类具有以下构造函数:

// initialize with range [First, Last)
template<class InputIterator>
   vector(
      InputIterator First,
      InputIterator Last
   );

To make your sample compiling you need to include the following: 要进行示例编译,您需要包括以下内容:

#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm> // for std::copy

Since your identifiers not fully qualified you should add the following: 由于您的标识符不完全合格,因此应添加以下内容:

using namespace std;

Or fully qualify all STL identifiers. 或完全限定所有STL标识符。

And to change, I guess, 我想要改变

copy(words.begin(), words.end(), out)

to

copy(words.begin(), words.end(), oo)

Please copy and paste the compiler error. 请复制并粘贴编译器错误。 Also you are missing a number of headers such as algorithm and vector. 另外,您缺少许多标头,例如算法和向量。 You need a use namespace std declaration or use std:: to access the STL classes. 您需要使用命名空间std声明或使用std ::来访问STL类。

Once you provide more information we can give you more advice. 提供更多信息后,我们将为您提供更多建议。

Update: why is your error message referring to "Vector" (with capital), not vector (lower case)? 更新:为什么您的错误消息引用的是“向量”(大写),而不是向量(小写)?

图书标题存在某种合规性问题,因此我只包含了适当的标题就可以了。

The Vector class in std_lib_facilities.h defines three constructors, but not one which accepts a pair of iterators. std_lib_facilities.h中的Vector类定义了三个构造函数,但没有一个接受一对迭代器。 You can go ahead and add that to the header: 您可以继续将其添加到标题中:

template <class Iter>
Vector(Iter from, Iter to): std::vector<T>(from, to) {}

With this header you'll need to take into account that this is for hand-holding. 使用此标头时,您需要考虑到这是用于手持的。 operator[] in a real std::vector is not supposed to do range-checking. 实际std :: vector中的operator []不应进行范围检查。 (Why not just teach beginners to use vector::at instead, until they get the idea that it might be better to stay in bounds...?) (为什么不只是教导初学者使用vector :: at,直到他们意识到保持边界可能更好……?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM