列表初始化 [英] List initializer
问题描述
我试图声明一个 std :: initializer_list
类型的迭代器,因为它在此回答。
I am trying to declare an iterator of type std::initializer_list
as it is described in this answer.
但我总是得到这个错误:
But I always get this error:
error: 'iterator' is not a member of 'std::initializer_list<int>'
有人可能会指导我如何声明一个迭代器类型 std :: initializer_list
?
May someone guide me how to declare an iterator of type std::initializer_list
?
EDIT:
此代码例如将不工作。此外,我使用的编译器支持新的标准草案
This code for example will not work. In addition, The compiler that I use supports the new standard draft
int findCommon(std::initializer_list<int> nums)
{
std::initializer_list<int>::iterator it;
for (it = nums.begin() ; it != nums.end() ; ++it)
{
std::cout << *it << std::endl;
}
return 1;
}
推荐答案
p>
You need to add
#include <initializer_list>
并且对于此示例也可能
#include <iostream>
还要确保使用 -std = c ++ 0x
切换到g ++。你需要至少g ++版本4.4。 当使用MacPorts编译器时,编译器调用(错误的假设)。 g ++ - mp-4.5
或 g ++ - mp-4.4
。
Also make sure you are using the -std=c++0x
switch to g++. And you need at least g++ version 4.4. When using the compiler from MacPorts, the compiler is called (Incorrect assumption).g++-mp-4.5
or g++-mp-4.4
.
我注意到一些最近修复的错误与 initializer_list
GCC bugzilla ,因此我建议您使用最新版本的GCC 4.5.1 。因为当使用这个版本,我能够编译两个你的例子没有问题。
I noticed some recently fixed bugs related to initializer_list
on the GCC bugzilla, so I recommend getting the newest version of GCC, 4.5.1. Because when using this version I am able to compile both your examples without problems.
这篇关于列表初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!