【发布时间】:2018-01-09 02:28:41
【问题描述】:
我在 xcode 中运行此代码。为什么我的编译器一直抱怨映射分配
#include <iostream>
#include <map>
#include <deque>
using namespace std;
map<int,deque<int>> bucket;
deque<int> A{3,2,1};
deque<int> B;
deque<int> C;
bucket[1] = A;//Warning "C++ requires a type specifier for all declaration
bucket[2] = B;//Warning "C++ requires a type specifier for all declaration
bucket[3] = C;//Warning "C++ requires a type specifier for all declaration
int main() {
for (auto it:bucket)
{
cout << it.first << "::";
for (auto di = it.second.begin(); di != it.second.end(); di++)
{
cout << "=>" << *di;
}
cout << endl;
}
return 0;
}
好像我在 main 里面做同样的事情一样完美
#include <iostream>
#include <map>
#include <deque>
using namespace std;
map<int,deque<int>> bucket;
deque<int> A{3,2,1};
deque<int> B;
deque<int> C;
int main() {
bucket[1] = A;
bucket[2] = B;
bucket[3] = C;
for (auto it:bucket)
{
cout << it.first << "::";
for (auto di = it.second.begin(); di != it.second.end(); di++)
{
cout << "=>" << *di;
}
cout << endl;
}
return 0;
}
输出
1::=>3=>2=>1
2::
3::
Program ended with exit code: 0
这是我遗漏的东西吗?无法理解这种行为。 任何建议、帮助或文档。我查看了类似的问题,但没有得到满意的答案
【问题讨论】:
-
在 C++ 中确实没有函数之外的可执行代码。
-
双端队列
A{3,2,1};为什么这行得通? -
因为那是一个变量的声明。这就是“可执行代码”的概念变得有点模糊的地方。全局变量调用了它的构造函数,在这种情况下,它是初始化列表构造函数。
标签: c++ dictionary stl deque