在使用迭代器迭代地图时,在unordered_map中以特定顺序插入的键是否会以相同的顺序出现?
例如:如果我们在B中插入(4,3),(2,5),(6,7)并且迭代如下:
for(auto it=B.begin();it!=B.end();it++) {
cout<<(it->first);
}
Run Code Online (Sandbox Code Playgroud)
会给我们4,2,6或钥匙可以按任何顺序出现吗?
Trie实现的以下代码在调用函数insert时抛出浮点异常.for循环内部的行检查现有节点是问题所在.
struct Node {
char c;
bool isend;
unordered_map<int, struct Node*> map;
};
void insert(struct Node* root, string contact) {
int size = contact.size();
char ch;
for (int i = 0; i < size; i++) {
ch = contact[i];
// this check is creating problem
if (root->map.find(ch) == root->map.end()) {
struct Node* tmp = (struct Node*) malloc(sizeof(struct Node));
tmp->c = ch;
if (i == (size - 1)) {
tmp->isend = true;
} else {
tmp->isend = false;
}
root->map.insert(make_pair(ch, tmp)); …Run Code Online (Sandbox Code Playgroud)