STL地图的奇怪输出

问题描述 投票:0回答:1

C程序员试图调用C ++映射(使用相关数组或散列的功能)。

字符串只是一个开头,将继续哈希二进制字符串。卡在第一步。不知道为什么这个程序的输出只会返回0。

#include <string.h>
#include <iostream>
#include <map>
#include <utility>
#include <stdio.h>

using namespace std;

extern "C" {
int get(map<string, int> e, char* s){
    return e[s];
}
int set(map<string, int> e, char* s, int value) {
    e[s] = value;
}
}

int main()
{
   map<string, int> Employees;
    printf("size is %d\n", sizeof(Employees));
   set(Employees, "jin", 100);
   set(Employees, "joe", 101);
   set(Employees, "john", 102);
   set(Employees, "jerry", 103);
   set(Employees, "jobs", 1004);
    printf("value %d\n", get(Employees, "joe"));

}

谢谢。

c++ stl
1个回答
0
投票

发现了两个错误(还):

printf("size is %d\n", sizeof(Employees));

必须

printf("size is %d\n", Employees.size());

sizeof为您提供对象的大小,而不是内部元素的数量。


int set(map<string, int> e, char* s, int value) 

必须

int set(map<string, int> &e, char* s, int value)

否则你将复制到函数,而不是原件(如在C中)。复制将在离开函数范围后丢弃。原件没有改变。

© www.soinside.com 2019 - 2024. All rights reserved.