typeof运算符似乎不适用于TC或ideone编译器

时间:2014-08-16 06:24:33

标签: c++ typeof

#include <bits/stdc++.h>
--------------------------
---------------------------
#include<map>
-----------------
#include<iterator>
-------------------------
using namespace std;
------------------------------------

int main(){

map<int,int>M;
M[1]=2;
M[3]=4;
M[4]=5;
M[2]=3;
for(typeof(M.begin()) it = (M).begin(); it != (M).end(); ++it);
   cout<<it->second;
return 0;
}

3 个答案:

答案 0 :(得分:3)

C ++语言没有typeof运算符。它是C语言的GCC特定扩展。具有相同(或非常相似)功能的C ++ 11功能是decltype。在C ++代码中使用decltype,而不是typeof

但是,在您的特定情况下,auto可能是更合适的选择。

答案 1 :(得分:2)

我认为你可以使用auto。

for (auto it = m.begin(); it != m.end(); ++it)
    cout << it->second << endl;

或者您可以使用迭代器。

map<int, int>::iterator it;

for (it = m.begin(); it != m.end(); ++it)
    cout << it->second << endl;

答案 2 :(得分:0)

使用在线编译器,您的里程可能会有所不同。

使用man gcc | grep "\-std"告诉我们:

   -std=
       extensions that do not contradict it.  For example, -std=c90 turns
       the standard. For example -std=gnu90 -Wpedantic warns about C++
       style // comments, while -std=gnu99 -Wpedantic does not.
           GNU dialect of -std=c++98.  This is the default for C++ code.

这意味着-std=gnu++98是未指定-std时的默认模式。虽然指定任何c++xx标准将导致它不能编译。您可以使用gnu++xx代替(GNU extensions启用此功能。)这意味着您的代码无法使用Ideone的C ++ 11模式进行编译(添加-std=c++11) ,但它将在C ++ 4.8.1模式下(很可能不添加任何-std开关)。使用Coliru,您可以自己修改调用并测试这些选项。

或者,如果您选择不使用gnu++xx,请尝试__typeof__

正如其他人已经说过的那样,如果你是C ++ 11模式,那么在迭代器循环中使用auto是惯用的:

for (auto it = map.begin(); it != map.end(); ++it)
相关问题