3

以下代码测试使用 std::map 和 std::string 作为键:

#include <stdio.h>
#include <map>
#include <string>
using namespace std;

typedef map<string, int> test_map_t;

int main(int argc, char **argv) {
    test_map_t test_map;

    test_map["test1"]= 1;    
    test_map["test2"]= 2;
    test_map["test3"]= 3;    

    string tmp= "test1";
    printf("%s : %d \n", tmp.c_str(), test_map[tmp]);

    return 0;
}

当使用普通 gcc 编译时,该测试将按预期打印出“test1 : 1”。但是,当使用 alchemy 编译时,它会打印“test1 : 3”(!)。这里有些不对劲。

是否有任何解决方法或者我只是卡住了?

4

3 回答 3

2

类字符串在炼金术中被破坏。运算符副本 (=) 中存在错误。地图适用于其他班级

于 2010-09-24T08:22:32.550 回答
1

当然看起来像一个错误。

通常,源代码(标头)是 STL 分发的一部分——您可以逐步了解发生了什么吗?也许将源代码与 GCC 版本进行比较。

如果确认,您似乎有一个铸铁箱可以将其带到供应商处进行修复。

于 2010-09-20T23:32:51.330 回答
0

你不应该使用 cstdio 吗?但是您的代码与 gcc 版本 4.4.2 20091027 完美配合,我已经对其进行了测试。它是完整的代码还是可能覆盖堆栈的东西。

#include <cstdio>
#include <map>
于 2010-09-21T02:16:22.383 回答