How to declare boost unordered_multimap - c++

I'm trying to use the boost unordered_multimap class and I'm having trouble declaring it. The error follows the code.
#include <iostream>
#include <map> // header file needed for to use MAP STL
#include <boost/unordered_map.hpp>
using namespace std;
int main(void)
{
map<int, map<int, map<int,int> > > _3dstl;
_3dstl[0][0][0] = 10;
cout<<_3d[0][0][0]<<endl;
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
unordered_map _2d;
return 0;
}
This is the error:
||In function 'int main()':|
|17|error: template argument 2 is invalid|
|17|error: template argument 5 is invalid|
|17|warning: 'typedef' was ignored in this declaration|
|18|error: 'unordered_map' was not declared in this scope|
|18|error: expected ';' before 'location3d'|
||=== Build finished: 4 errors, 1 warnings ===|

Change this line:
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
To this:
typedef boost::unordered_multimap<int, boost::unordered_multimap<int, int> > unordered_map;
The second typedef is not necessary and a syntax error. Also in C++2003 you have to watch out for >> in template declarations.
Also, please use a different name then unordered_map for the typedef, since this will collide with std::unordered_map if you use using namespace std; (which is IMO a bad practice). A suggestion would be intmap2d or something.

Related

Using vector as matrix?

How may I define a vector in C++11 such that its size is 4*5 so I can treat it like a matrix?
(I mean using operator [] like the following)
mat[2][3];
Update:
The following gives me error:
#include <memory>
class Test{
std::vector<int> vect;
};
Error message:
implicit instantiation of undefined template 'std::__1::vector<int, std::__1::allocator<int> >'
std::vector<int> vect;
You just need a nested vector, like this:
auto v = std::vector<std::vector<int>>(4, std::vector<int>(5));
and you can then index it like v[0][0].
The error implicit instantiation of undefined template std::__1::vector ... means std::vector is unknown to the compiler.
You need to #include <vector>

Using an array as a map key is not working with C++ 11 compiler command?

I need to use an array as the map key, but I receive compiler errors indicating that the map declaration does not name a type.
I use the code in a similar question, but the code does not compile even when I have chosen the -std=c++0x or -std=c++11 compiler commands.
The code I used is:
typedef std::array<unsigned int, 3> alphabet;
std::map<alphabet, std::string> dictionary;
dictionary[{{1, 0, 8}}] = "hello";
The error is:
error: 'dictionary' does not name a type| error: expected
unqualified-id before ']' token| ||=== Build finished: 2 errors, 0
warnings (0 minutes, 1 seconds) ===|
I see little on this topic even when searching Google. I am using CodeBlocks as my IDE and chosen the compiler commands mentioned above.
I think the error may be because you're trying to assign to dictionary in file scope. As pointed out, variables should be initialized in global scope, i.e.:
std::map<alphabet, std::string> dictionary = { {{1,0,8}, "hello"} };
Otherwise, you should put it in block scope, i.e. in a main().
#include <array>
#include <map>
typedef std::array<unsigned int, 3> alphabet;
std::map<alphabet, std::string> dictionary;
int main()
{
dictionary[{{1, 0, 8}}] = "hello";
}
As a side note, it seems that the braces can be elided. You do not need two sets of braces. dictionary[{1, 0, 8}] will suffice.
How you compare the arrays for the map sort?
I guess you should supply compare method like this:
struct ltarray
{
bool operator()(const alphabet& s1, const alphabet& s2) const
{
//how you compare???
return (s1<s2);
}
};
and you need to init your map template with the compare method:
std::map<alphabet, std::string, ltarray> dictionary;

Error using unordered_set_of with Boost.Bimap

I'm attempting to follow this example from the documentation (see typedef for word_counter).
#include <string>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
typedef boost::bimap
<
boost::bimap::unordered_set_of< std::string >,
std::string
> MyBimap;
Error thrown is
test.cpp:11:1: error: wrong number of template arguments (1, should be 5)
In file included from /usr/include/boost/bimap.hpp:13:0, from test.cpp:3:
/usr/include/boost/bimap/bimap.hpp:133:7: error: provided for ‘template class boost::bimaps::bimap’
test.cpp:11:10: error: invalid type in declaration before ‘;’ token
You have a typo.
Instead of
boost::bimap::unordered_set_of< std::string >,
use
boost::bimaps::unordered_set_of< std::string >,
in the template.
It will compile then.

boost::multi_index error "template argument 'x' is invalid"

So I made some kind of database with boost_multi_index like this:
#include <boost\multi_index_container.hpp>
#include <boost\multi_index\ordered_index.hpp>
#include <boost\multi_index\member.hpp>
using namespace boost::multi_index;
using namespace std;
struct list_entry{
int id;
string name;
string* data;
};
typedef multi_index_container<list_entry,
indexed_by<
ordered_unique< tag<int>, member<list_entry, int, &list_entry::id>>, // unique id
ordered_unique< tag<string>, member<list_entry, string, &list_entry::name>>, // unique name
ordered_non_unique< tag<string*>, member<list_entry, string*, &list_entry::data>> // some data associated with the id/name
>
>table;
class Database
{
private:
table music, names;
typedef table::index<int>::type list_id;
typedef table::index<string>::type list_string;
//some more code here
};
and it compiles fine with Visual Studio 2010.
However I wanted to switch my project over to Code::Blocks with MinGW and it seems like he's not so happy with that, this is the compile log:
.\source\db.h|19|error: template argument 3 is invalid|
.\source\db.h|15|error: template argument 2 is invalid|
.\source\db.h|15|error: template argument 1 is invalid|
.\source\db.h|14|error: template argument 2 is invalid|
.\source\db.h|13|warning: 'typedef' was ignored in this declaration [enabled by default]|
.\source\db.h|24|error: 'table' does not name a type|
.\source\db.h|25|error: 'table' does not name a type|
.\source\db.h|26|error: 'table' does not name a type|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|222|warning: 'boost::system::posix_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|223|warning: 'boost::system::errno_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|224|warning: 'boost::system::native_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|244|warning: 'boost::asio::error::system_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|246|warning: 'boost::asio::error::netdb_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|248|warning: 'boost::asio::error::addrinfo_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|250|warning: 'boost::asio::error::misc_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\ssl\error.hpp|34|warning: 'boost::asio::error::ssl_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\detail\winsock_init.hpp|116|warning: 'boost::asio::detail::winsock_init_instance' defined but not used [-Wunused-variable]|
||=== Build finished: 7 errors, 10 warnings (0 minutes, 15 seconds) ===|
I'm completely clueless since the error is not any more specific than that and I couldn't find any related problem.
So I hope someone can give me an answer here, if more information is required I will edit it in afterwards.
I was able to solve this issue by using BOOST_MULTI_INDEX_MEMBER like this:
typedef multi_index_container<list_entry,
indexed_by<
ordered_unique< tag<int>, BOOST_MULTI_INDEX_MEMBER(list_entry, int, id)>,
ordered_unique< tag<string>, BOOST_MULTI_INDEX_MEMBER(list_entry, string, name)>,
ordered_non_unique< tag<string*>, BOOST_MULTI_INDEX_MEMBER(list_entry, string*, data)>
>
>table;

Why do I get "Expected ;" error and "Variable not declared in scope" error?

i have following code
#include <iostream>
#include <set>
#include <string>
using namespace std;
template<class Container>
void print(const Container &c)
{
Container::const_iterator itr;
for (itr=c.begin();itr!=c.end();itr++){
cout<<*itr<< '\n';
}
}
int main(){
set<string,greater<string>>s;
s.insert("georgia");
s.insert("saqartvelo");
print(s);
return 0;
}
but errors are
reverse.cpp: In function ‘void print(const Container&)’:
reverse.cpp:9: error: expected ‘;’ before ‘itr’
reverse.cpp:10: error: ‘itr’ was not declared in this scope
reverse.cpp: In function ‘int main()’:
reverse.cpp:17: error: ‘s’ was not declared in this scope
reverse.cpp:17: error: ‘>>’ should be ‘> >’ within a nested template argument list
What might cause this and how do I solve it?
You need typename Container::const_iterator instead of Container::const_iterator.
At the point the compiler is reading your code, it doesn't know that Container has such a type (it is a so-called dependent name).
Alexandre is right about the first two errors. The last two are due to an annoying syntax limitation of C++: you need to have a space in between the two closing brackets in the template expression:
set<string,greater<string> > s;
Otherwise, C++ interprets it as the right shift >> operator.