空括号{}作为范围的结尾 [英] empty curly bracket {} as end of range

查看:78
本文介绍了空括号{}作为范围的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在优胜美地的XCode上运行.

I'm running on XCode, Yosemite.

以下代码已编译但在运行时崩溃,为什么?

Following code compiled but crashes at run time, why?

我故意在第二个std :: copy中使用"{}"作为范围的结尾".

I intentionally used "{}" in the second std::copy as the "end of range".

由于一个使用"{}"作为默认构造的流迭代器作为范围的结尾"的工作示例,因此我对该代码进行了实验.

I experimented this code because of a working example that used "{}" as "default constructed stream iterator as end of the range".

那为什么为什么(请参阅第2个代码)一个却起作用(而第一个代码)却失败了?

#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // copy the elements of coll1 into coll2 by appending them
    vector<int> coll2;
    copy (coll1.cbegin(), coll1.cend(),    // source
          back_inserter(coll2));           // destination

    vector<int> coll3;
    copy (coll1.begin(), {},
          back_inserter(coll3));

}

以下代码来自C ++标准库第二版.

Following code is from The C++ Standard Library second edition.

带有"//源末尾"的行可以是"istream_iterator()",也可以仅仅是"{}".

The line with "// end of source" could be either "istream_iterator()," or simply "{},"

两者之所以有效,是因为:从书中引用

both works because: quoted from the book

",请注意,自C ++ 11起,您可以传递空的花括号而不是默认构造的流迭代器作为范围的结尾.之所以起作用,是因为定义了源范围结尾的参数的类型是从定义源范围起点的上一个参数推导出来的."

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition"
 * by Nicolai M. Josuttis, Addison-Wesley, 2012
 *
 * (C) Copyright Nicolai M. Josuttis 2012.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    vector<string> coll;

    // read all words from the standard input
    // - source: all strings until end-of-file (or error)
    // - destination: coll (inserting)
    copy (istream_iterator<string>(cin),    // start of source
          {},       // end of source
          back_inserter(coll));             // destination

    // sort elements
    sort (coll.begin(), coll.end());

    // print all elements without duplicates
    // - source: coll
    // - destination: standard output (with newline between elements)
    unique_copy (coll.cbegin(), coll.cend(),           // source
                 ostream_iterator<string>(cout,"\n")); // destination
}

推荐答案

第一个失败,因为迭代器的类型不是stream_iterator.

The first one failed because the type of your iterator is not a stream_iterator.

对于stream_iterator情况,默认构造函数具有特殊含义-EOF.代表容器末端的迭代器不是默认构造的. (实际上,对于简单容器,迭代器可以只是指针).

For the stream_iterator case the default constructor has a special meaning - EOF. The iterator representing the end of a container is not default constructed. (In practice for simple containers iterators can be just pointers).

除流迭代器外,默认构造迭代器通常没有多大意义,并且在这种情况下不具有您想要的语义.

Default constructing iterators other than stream iterators don't make much sense usually and doesn't have the semantics you'd want in this instance.

(除了流迭代器外,boost中的其他一些迭代器也遵循与流迭代器相同的模式.)

(Some other iterators from boost besides stream iterators do follow the same pattern as stream iterators do).

这篇关于空括号{}作为范围的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆