#include <iostream>
#include <algorithm>
#include <climits>
#include <map>
#include <unordered_map>
using namespace std;
int main()
{
std::map<int, std::unordered_map<std::pair<int, int>, int>> region;
region[0].insert(make_pair(make_pair(1, 1), 1));
return 0;
}
I am writing the above code and it don't work as expected, How can I fixed it? The error is " error C2064: term does not evaluate to a function taking 1 arguments"
There is no specialization of std::hash for std::pair, so it can't be used as a key for std::unordered_map unless you provide a custom hash function.
Related
I am trying to use boost::container::map. During inserting data, the error "insert is ambiguous" is shown.
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert("Ram",0);
}
Your style of inserting is not correct. I provide the code:
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert(std::pair<const std::string, int>("Ram",1));
std::cout<< map["Ram"];
return 0;
}
The following gives an error for the code mentioned below.
Where I have gone wrong ?
error: ‘function’ is not a member of ‘std’
I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4));
my_pq.push(IceCream(33));
my_pq.push(IceCream(9));
cout << my_pq.top() << endl;
return 0;
}
#include <functional>
You need this include to get access to std::function
See: http://en.cppreference.com/w/cpp/utility/functional/function
What is the fault of this bind operation with visual studio 2013 for error C3867 ?
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
int main()
{
std::map<int, int> m1, m2;
std::vector<std::map<int, int> *> pM;
std::for_each(pM.begin(), pM.end(),
std::bind(std::map<int, int>::erase , 1));
}
If you want to delete each map's element (which key is 1) in the vector. Here is a sample.
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
using namespace std::placeholders;
int main()
{
std::map<int, int> m1 {{1,2},{2,3},{3,4}}, m2 {{2,3},{1,5},{8,9}};
std::vector<std::map<int, int> *> pM {&m1,&m2};
size_t(std::map<int,int>::*pf)(const int& key)=&std::map<int,int>::erase;//note this line.
std::for_each(pM.begin(), pM.end(),std::bind(pf,_1,1));//note this line
for(auto& ele:m1)
{
cout<<ele.first<<","<<ele.second<<endl;
}
for(auto& ele:m2)
{
cout<<ele.first<<","<<ele.second<<endl;
}
}
As chris says:
For one thing, member functions do not decay into member function
pointers. For another, std::map::erase is overloaded.
We must use & to get member function pointer,and must tell compiler which overload function you want to choice.
So write code like this:
size_t(std::map<int,int>::*pf)(const int& key)=&std::map<int,int>::erase;
And further:
std::bind(pf,_1,1)
member function need object as its implicit first parameter, _1 do this work.The last parameter 1 is int as the key pass to map erase member function.
So in my cpp file I'm trying to declare a map as follows:
map<string, vector<myStruct>> myMap;
At the top of my file I have written using namespace std and I also have #include <string>
.
However I'm getting these weird errors:
error: ISO C++ forbids declaration of ‘map’ with no type
I don't know how to fix it. If I write #include <map> that just causes the compiler to freak out.
do you have #include <map>? rest looks valid,
however you might need to add a space if your C++ standard is not C++11:
#include <map>
#include <vector>
#include <string>
using namespace std;
map<string, vector<myStruct> > myMap;
^^^
even better not use namespace std:
#include <map>
#include <vector>
#include <string>
std::map<std::string, std::vector<myStruct> > myMap;
You need to include map header file.
#include <map>
Meanwhile, in case you are not using C++11, you need a space:
map<string, vector<myStruct> > myMap;
//^^
You should also include <map>. std::map is introduced through this header.
Furthermore, using namespace std is considered a bad practice. You should either have a using statement or use the prefix the name with std:: to denote a fully-qualified identifier:
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::vector<myStruct>> myMap;
Note, the lack of a using statement ;)
#include <vector>
#include <string>
#include <map>
#include <iostream>
typedef int myStruct;
std::map<std::string, std::vector<myStruct>> myMap;
int
main()
{
std::vector<myStruct> testMe = { 1, 2, 3};
myMap["myTest"] = testMe;
std::cout << myMap.size() << std::endl;
return(0);
}
sorry if this is a newB question,
please conider the following code:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
using boost::multi_index_container;
using namespace boost::multi_index;
typedef multi_index_container<
std::string,
indexed_by<
sequenced<>,
ordered_non_unique<identity<std::string> >
>
> text_container;
typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
int main()
{
std::string text=
"Alice was getting very tired of sitting by her sister";
text_container tc;
text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
int i=0;
for(text_container::iterator bb=tc.begin();bb!=tc.end();bb++,i++)
// std::cout << *bb << std::endl;
std::cout << tc[i] << std::endl;
return 0;
}
I would like to access, for example, the 10th element in the container. do I still have to use an iterator? or is there away to access a specific sequenced element in array-like fashion(or any other way...please suggest)
Appreciate your help
vahid
You can specify a random access index for the multi index by changing the line sequenced<>, to random_access<>, (you'll need to #include <boost/multi_index/random_access_index.hpp>). This will allow you to remove the iterator in the for loop.
For further details, see the documentation.