- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 XCode、Yosemite 上运行。
以下代码已编译但在运行时崩溃,为什么?
我故意在第二个 std::copy 中使用“{}”作为“范围结束”。
我试验这段代码是因为有一个工作示例使用“{}”作为“默认构造的流迭代器作为范围的末尾”。
那么,为什么那个(见第二个代码)一个工作正常而这个(第一个代码)一个失败了?
#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));
}
以下代码来自 The C++ Standard Library 第二版。
带有“//end of source”的行可以是“istream_iterator()”或简单的“{}”
两者都有效,因为:引自书中
“请注意,自 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
。
对于 stream_iterator
情况,默认构造函数具有特殊含义 - EOF。表示容器末尾的迭代器不是默认构造的。 (在实践中,对于简单的容器,迭代器可以只是指针)。
除流迭代器之外的默认构造迭代器通常没有多大意义,并且在这种情况下不具有您想要的语义。
(除了流迭代器之外,来自 boost 的其他一些迭代器确实遵循与流迭代器相同的模式)。
关于c++ - 空花括号 {} 作为范围的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30124122/
我用 redis 后端运行 celery。 我想在 centos 6.2 上运行 celery 花作为守护进程。 我知道花是 Tornado 应用程序,所以我应该使用一个进程来运行 Tornado 应
我在一台服务器上有几个项目,它们使用具有不同 BROKER_URL 的 celery 包。Flower 允许一个 BORKER_URL 作为命令选项: celery flower --broker=a
我正在使用芹菜和花卉。当我访问Flower中的“任务”标签时,我可以看到我的任务正在注册,甚至可以在“状态”列中看到“成功”标签以及所有内容。 但是,在“监视器”选项卡上,所有图形(“成功任务”,“失
我正在尝试创建一个有 5 个叶子的 css 花 - 没有任何有希望的尝试。我正在尝试创建什么:有 5 片叶子的 CSS 花,每片叶子可以有三种不同的状态(小、中、大)。我找到了 http://ross
如果我有一个变体,像这样: using my_variant = boost::variant; 有没有一种简单的方法可以将变体可以包含的类型提取到 Boost.Hana 元组中,以便满足以下条件:
调用 Flower API's /api/task/apply/* 只是挂起而没有返回结果,即使任务已根据 Web UI 处理并成功。 重现错误: # hangs even though task s
我有一个模板函数,默认情况下没有定义,但它由某些类型特化: template auto foo(bar &, const T &) -> void; template <> auto foo(bar
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我是一名优秀的程序员,十分优秀!