简体   繁体   English

GCC中的unordered_map错误

[英]unordered_map error in GCC

When was the unordered_map concept built into g++? 什么是g ++中内置的unordered_map概念?

Because the following code throws an error. 因为以下代码引发错误。

#include<iostream>
#include<unordered_map>
#include<stdio.h>

using namespace std;

std::unordered_map<std::int,int> mirror;

mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;

int main(void)
{
    std::cout<<mirror['A'];
    std::cout<<mirror['B'];
    std::cout<<mirror['C'];
    return 0;
}

I am compiling the code as follows: 我正在编译代码如下:

g++ -c hashexample.cpp
g++ -o result hashExample.o
./result

The error I got is this: 我得到的错误是这样的:

inavalid types int[char[ for aaray subscript inavalid类型int [char [用于aaray下标

What is the fix for this? 有什么办法解决这个问题?

The problem is your assignment. 问题是你的任务。 You cannot assign values to your map in this place. 您无法在此处为地图指定值。 C++ is not a script language. C ++不是一种脚本语言。
This program works fine on my machine with gcc4.6: 这个程序在我的机器上使用gcc4.6正常工作:

#include<iostream>
#include<unordered_map>

std::unordered_map<int,int> mirror;

int main() {
    mirror['A'] = 'A';
    mirror['B'] = '#';
    mirror['E'] = 3;

    std::cout<<mirror['A'];
    std::cout<<mirror['B'];
    std::cout<<mirror['C'];
}

First, as mkaes points out, you cannot put assignments outside functions, so you have to put it in any, for example main . 首先,正如mkaes指出的那样,你不能将赋值放在函数之外,所以你必须将它放在任何函数中,例如main

As for unordered_map , for recent versions of gcc, if you don't want to go into C++11, you can use the TR1 version of unordered_map : 对于unordered_map ,对于最新版本的gcc,如果你不想进入C ++ 11,你可以使用unordered_map的TR1版本:

#include <tr1/unordered_map>

and the type std::tr1::unordered_map . 和类型std::tr1::unordered_map You know, C++11 supersedes all this, but you will (at least in GCC) get this working. 你知道,C ++ 11取代了所有这些,但你会(至少在GCC中)得到这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM