STL映射的奇怪输出
strange output of STL map
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 %dn", sizeof(Employees));
set(Employees, "jin", 100);
set(Employees, "joe", 101);
set(Employees, "john", 102);
set(Employees, "jerry", 103);
set(Employees, "jobs", 1004);
printf("value %dn", get(Employees, "joe"));
}
谢谢。
发现了两个错误:
printf("size is %dn", sizeof(Employees));
必须是
printf("size is %dn", Employees.size());
sizeof提供的是对象的大小,而不是其中的元素数。
int set(map<string, int> e, char* s, int value)
必须是
int set(map<string, int> &e, char* s, int value)
否则,你会给函数一个副本,而不是原来的(如C)。离开函数范围后,副本将被丢弃。原件没有改动。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 为什么 du -sh 输出错误大小的内存映射文件
- 在映射中获取已知哈希输出的值
- 映射C 的输出错误
- 如何将std::映射输出到二进制文件
- STL映射的奇怪输出
- 输出与映射的交互器
- 如何合并2个std::映射,并在第三个映射中输出结果
- C++映射输出格式化垃圾
- 研究一个简单的代码反汇编输出和内存映射
- 如何用一对向量输出一个映射
- 输出映射
- c++ STL映射迭代器输出十六进制值
- 将字符串映射到整型CPP -输出在执行期间挂起
- 带有指针的映射的C++输出
- STL映射输出迭代器查找值
- 输出无序映射内的结构变量