#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void fun(const unordered_map<int, vector<int>>& direct_paths) {
const int var = direct_paths[1][0];
cout << var;
}
int main()
{
unordered_map<int, vector<int>> a;
a[1] = vector<int> {1,2,3};
fun(a);
return 0;
}
The above code outputs the following error:
error: passing ‘const std::unordered_map<int, std::vector<int> >’ as ‘this’ argument discards qualifiers [-fpermissive]
const int var = direct_paths[1][0];
^
Where as The below code doesn't output any compilation error:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void fun(const vector<int>& direct_paths) {
const int var = direct_paths[1];
cout << var;
}
int main()
{
vector<int> a;
a = vector<int> {1,2,3};
fun(a);
return 0;
}
Questions:
- Can I assign the value in a key-value pair of an unordered_map somehow?
- Why is assigning an integer from vector taken from const unordered_map<int, vector&> disallowed? & from const vector allowed?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire