English 中文(简体)
在何种情况下,应当使用“集器”增量?
原标题:In what situation should ostream_iterator increment be used?
std::istream_iterator<std::string> ist(std::cin);
std::istream_iterator<std::string> eof;
std::vector<std::string> str_vec(ist, eof);
std::ofstream ofs("a");
if (!ofs) {
    throw std::runtime_error("Open file failed.");
}
std::ostream_iterator<std::string> ost(ofs, "
");
for (size_t index = 0; index != str_vec.size(); ++index) {
    //*ost = str_vec[index];
    *ost++ = str_vec[index];
}

我得出同样的结果,无论我是否使用*st++。 我知道Istream_iterator increment的含义。 但是,在何种情况下,应当利用“放电器”增量?

感谢!

最佳回答

进一步的试验可能表明,你甚至不需要参照导师开展工作。

ost = str_vec[index];

所有这些绝缘方法都有必要使下游探测器与其他探测器相类似的接口。

相反,你可以使用<代码>std:copy算法。 (占所有<条码>的>> 回答你的问题: 你们不需要与你们自己的法典中的那些主人交谈!

 std::copy(str_vec.begin(), str_vec.end(), std::ostream_iterator<std::string>(ofs, "
"));

考虑到复印机功能模板是如何书写的,在需要加插和疏漏之处,可能变得很清楚:

template <class InIter, class OutIter>
void copy(InIter begin, InIter end, OutIter result)
{
    for (InIter it = begin; it != end; ++it)
        *result++ = *it;  // <-- here, result might be almost any kind of iterator
} 
问题回答

增量操作员很可能是<代码>ostream_iterator的绝缘,但必须使运营商满足产出机的要求。 例如,点工是有效输出器,必须加以加固。

您的算法是推算“流出器”。 它应当增减 输出到<>。 因此,如果你想得出下一个内容的话,总是会增加你的产出。 这样,你的算法将支持<代码>std:ostream_iterator和std:vector< T>:iterator and a T* pointer. 缩略语





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?