- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
正如所怀疑的那样,用花括号初始化 std::pair
不起作用,因为 std::pair
不是聚合。
std::pair <int, int> p = {1,2}; // Doesn't work
但是,初始化 std::pair
数组效果很好(在 gcc 4.9
中有警告)
std::pair <int, int> a_p[] = {
{1,2},
{3,4},
{5,6}
}; // Works fine
为什么会这样?
编辑:此问题已被标记为可能重复 C++11 aggregate initialization for classes with non-static member initializers但是,这个问题没有谈论非静态成员初始化器,据我所知,std::pair
有一个用户定义的构造函数。引自 http://en.cppreference.com/w/cpp/header/utility ,
template <class T1, class T2>
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(const pair&) = default;
pair(pair&&) = default;
constexpr pair();
pair(const T1& x, const T2& y);
template<class U, class V> pair(U&& x, V&& y);
template<class U, class V> pair(const pair<U, V>& p);
template<class U, class V> pair(pair<U, V>&& p);
template <class... Args1, class... Args2>
pair(piecewise_construct_t,
tuple<Args1...> first_args, tuple<Args2...> second_args);
pair& operator=(const pair& p);
template<class U, class V> pair& operator=(const pair<U, V>& p);
pair& operator=(pair&& p) noexcept(see below);
template<class U, class V> pair& operator=(pair<U, V>&& p);
void swap(pair& p) noexcept( noexcept(swap(first, p.first)) &&
noexcept(swap(second, p.second)) );
};
最佳答案
自 C++11 发布以来,类类型的聚合式初始化(“统一初始化”)在过去 5 年中一直是合法的。
旧版本的 gcc 默认使用古老的 C++03 标准(或更旧的 C++98),但了解 C++11,因此在某些情况下会应用 C++11 规则明确的。这是警告的意思:
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
一些更旧版本的 gcc 可能会应用它们自己的(C++11 之前的)扩展,因此会给出不同的警告。
您应该(至少)在 C++11 模式下编译,或者使用默认为 C++11 或 C++14 的现代编译器,例如 gcc 6.1。
关于c++ - std::pair 数组的聚合初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195356/
我正在学习使用 STL 的排序函数,方法是将它用于一些复杂的对 vector 。 我有以下 vector : vector > > > > 我需要先根据对中的第一个整数对元素进行排序,如果发现有 2
我想初始化: pair>,vector>> pvp; 所以对于所有的我: pvp.first[i].first = true; 和 pvp.second[i].first = false; 我知道您可
那为什么我们能实例化Pair却不能实例化Pair Pair p=new Pair(); 对比 Pair p=new Pair(); 我知道 意思是未知类型 --> 但不是 意思是一样的---> 有
这个问题在这里已经有了答案: How to have an unordered_map where the value type is the class it's in? (1 个回答) `std
我正在编写一个代码来处理warehouse[item[batch, qty]]的组合,然后将基于[batch, qty]的batch与qty的总和分组。我的代码是: package main impo
我想知道最好的类 java 容器是什么 > 我有一个作为键的 object1 以及一个 t1 和 t2。 我的类具有以下属性: public class Patient implements Exte
所以我有一些使用 Java 8 流的代码,而且它可以工作。它做的正是我需要它做的,而且清晰易读(这在函数式编程中很少见)。在子例程结束时,代码遍历自定义对类型的列表: // All names Hun
我正在声明一个字符串映射到一对对,如下所示: std::map, std::pair>> reference; 我将其初始化为: reference.insert
例如,镜像 pair 至 pair ,我可以像这样创建一个模板函数: template void mirror(const AB& ab,BA& ba){ ba.first=ab.secon
我正在使用 PyZMQ 创建一个简单的 PAIR/PAIR 通信原型(prototype)消息传递模式。 配对服务器 import zmq import random import sys impor
我正在尝试习惯 Kotlin 中的习语和快捷方式,我想知道是否有任何方法可以做到这一点。 val pairList = listOf(Pair(1, 2), Pair(5, 10), Pair(12,
我定义的 map 是这样的 map > hmap; 如果有一个pair(2,pair(3,4))如何得到2 3 4个值,itr->first, itr->第二个不工作 最佳答案 If there is
我希望能够对以下 vector 进行排序 -vector>> 基于 pair 的第一个元素,如果它们相等,则根据它们的第二个元素对它们进行排序,我如何使用 STL 在 C++ 中做到这一点构建? 这种
通过 PHP_PDO: fetchAssoc echo 从 MySql SELECT 查询得到一个 $.getJSON 结果使用 json_encode() 编辑,在 firebug 控制台中的输出如
假设我有以下功能: (defun f (v1 v2) ...) 我想简化以下代码: (lambda (pair) (apply #'f pair)) 此处的目标是创建一个函数,该函数接受两个值的列表并
对不起,我真的不知道怎么写这个标题。我可以想到这样做的代价高昂的方法,但我想看看是否有人可以指出一个优雅的解决方案,这里是: 我有很多成对关联的元素; 每个元素都有一个与其自身关联的数值,该数值表示它
我有一个使用以下方法用 Java 编写的 PreferencesManager: public void insert(Pair keyValue, boolean async) { 我正尝试从 k
这是 assigning-of-unordered-map-to-pair-of-objects 的后续问题.这是一个关于编译器错误解释的问题(而不是一个重复的问题,因为该问题已经得到了完整的回答)。
这是我的 map : map, pair > matchMap; 这是函数: void Schedule::studentSchedule() { string s, c; cout
#include #include #include using namespace std; int main() { map,pair> items; items.inser
我是一名优秀的程序员,十分优秀!