How do I access a priority_queue from another class? - c++

My code looks roughly like this:
#include <queue>
#include <iostream>
using namespace std;
int main() {
priority_queue <String> pq;
otherClass.qPusher();
pq.pop();
}
How does qPusher method access priority_queue pq?
I have tried to pass a reference of pq to qPusher, but I can't seem to get the syntax right. Does anyone know how to do this?

You would need to pass the queue to the class (probably as a constructor argument, likely as a reference, at the very least as a pointer).

Related

How to construct a vector of queues?

I need to construct a crawler working with front_queues and back_queues, which are vectors of queues. I've seen solutions in this question Vector of queues but my compiler complains the vec needs a constructor.
#include <vector>
#include <queue>
using namespace std;
vector<queue<int> > vec;
vec.push_back( queue<int>(0) );
// ^
// error: expected constructor, destructor, or type conversion before ‘.’ token
You need to put function calls inside blocks.
Try adding a main function :
#include <vector>
#include <queue>
using namespace std;
int main()
{
vector<queue<int> > vec;
queue<int> q;
vec.push_back(q);
return (0);
}
queuedoesn't have initializer list :
According to queue's constructor reference (Source), you can't use queue<int>(0) because no proper constructor would match.
However, you can use queue<int>(). It will create an empty queue.
Take a look at this online example : https://ideone.com/RbT1pD

C++ unordered map error

I'm trying to declare an unordered map into my program in which I will map them to tokens in another file.
I need a method which returns the Token type found in Token.h (which is an enum class)
What is confusing me is that, since I want to return the mapped Tokens from the unordered_map to the enum class, what should be the return type of the method? Also, it is stating that
error: 'unordered_map' does not name a type
I am rather new to C++ and am still finding it a bit hard in this case how I should declare methods. I've read that the unordered map should be declared INSIDE a method, but since I want the value returned by the map, which should be the return type?
I tried this
Test 1
Token Lexer::getTokenType()
{
unordered_map<string,Token> tokenType;
}
This outputs these errors:
Test 2 I tried this
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <wctype.h>
#include <map>
#include "lexer.h"
using namespace std;
long Row, Col, Offset;
unordered_map<string, Token> ProtectedWords
{
}
OR
unordered_map<string, Token>::Lexer::getTokenType()
{
}
still yielded the same
Its error:
I know these do sound stupid, but would you mind explain to me please? As in the tutorial I followed many are, yes, called inside a method, but even that did not work
You need to include <unordered_map>.
You'll also need to enable C++11 support, if you haven't already done so: for GCC, make sure the compiler arguments include -std=c++11 (or c++0x if you're using an old compiler).

Accessing second elements of the map with for_each or transform

I'd like to ask you how could I copy all second elements from
map<string, string> myMap
to
deque<string> myDeq
using for_each or transform without creating a functor. I tried it like in this question
transform(myMap.begin(), myMap.end(), back_inserter(myDeq), mem_fun_ref(&map<string, string>::value_type::second));
but it didn't work for me - I got error "Illegal use of this type".
The reason you get the error is because map<string, string>::value_type::second is not a member function. It is just a member variable of the std::pair template struct.
One possible solution without using functors is with the use of lambdas. But it's a C++11 feature so I don't know if that's what you want.
Take a look at the following example
#include <iostream>
#include <map>
#include <deque>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
int main()
{
map<string,string> myMap;
deque<string> myDeque;
myMap["key1"]="value1";
myMap["key2"]="value2";
transform(myMap.begin(),myMap.end(),back_inserter(myDeque),[](map<string,string>::value_type p){return p.second;});
copy(myDeque.begin(),myDeque.end(),ostream_iterator<string>(cout,"\n"));
}

How to use vector of instances of class what are declared within namespace

I can`t cope with the following situation:
class someName
{ public:
vector<compound_objectNS::Compound_object*> loadObjectsFromFile(char* fileName);
}
namespace compound_objectNS
{ class Compound_object {here goes it`s defenition}.
}
I get error from isense: "vector is not a template"
What I am doing wrong?
Help me please! Thanks in advance.
You need to #include <vector> before using std::vector<>.
If you didn't write using namespace std; or some similar using directive, you should write std::vector. Or maybe you simply forgot the #include <vector> at the beginning of the file.
Also, you have to provide a declaration for compound_objectNS before declaring that vector (either a full definition, either a forward declaration).

Does std::map<key, data> in C++ support native data types like Structures?

How do I map a key to a native data type like structure?
I wrote this snipped but I couldn't compile it. Do you have any ideas on how to fix it?
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
}list;
map<int,list> test_map;
int main(void)
{
cout <<"Testing"<< endl;
}
map resides in the std:: namespace. Two possible ways to fix this:
using namespace std;
// ...
map<int, list> test_map;
or
std::map<int, list> test_map;
I prefer the second method, but it's a purely personal choice.
On a related note, there is no real limitation on what you can put in a map, aside from the fact that they must be copyable/assignable, and that the key type must have a < operator (or you can also provide a comparer functor).
EDIT: Seems like <list> is included somewhere, either in <iostream> (unlikely) or <map> (strange but not impossible). A using namespace std will cause std::list to clash with your own struct. The solution: rename your struct, or remove the using namespace and put std:: where it's needed.
Added std where required.
Renamed list to mylist to avoid clash with std::list. Avoid typenames and variable names that clash with common usage.
Now compiles OK in VS2008.
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
} mylist;
std::map<int,mylist> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
There's no issue with using your struct in the STL containers provided it's copyable cleanly (copy constructor), assignable (implements operator=) and comparable (implements operator<).
A number of problems here:
You're missing either a using::std or std::map, so the compiler doesn't know what map<int,list> means.
Assuming you have a using namespace std declaration, your typedef list might collide with the STL collection of the same name. Change the name.
Your typedef struct _tag {...} tag; construct is an archaic holdover from the 80's. It is not necesarry, and frankly rather silly. It gets you nothing.
Here's your code fixed:
#include <map>
#include <iostream>
struct MyList
{
int a,b;
};
std::map<int,MyList> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
map<int, _list> test_map; or don't use list(much better) as a name of structure. (You probably also have
#include <list>
...
using namespace std;
somewhere in your code.
I would try to avoid using codepad at all.
I have done a couple of tests with your code and it seems that
it is adding an implicit (and unwanted) using namespace std --it does not require you to qualify map, cout or endl.
it is (probably) including more standard headers than you might want, including #include <list>.
That means that when the compiler looks at the code it is seeing two list, your version and the one in std. Because of the using directive, both are in scope in the line where you create the map and the compiler is not able to determine which to use.
Two simple things that you can do: change the name of your type for the simple test to something other than list (ouch! the tool forcing your naming choices!) or fully qualify the use:
#include <map>
struct list {
int a,b;
};
std::map< int, ::list > the_map;
// ...
Note that codepad is adding the include by itself and the using directive, so it will also compile:
struct list {
int a,b;
};
map<int,::list> the_map;
But that piece of code is wrong
You seem to be comming from C. Try this:
#include <map>
#include <iostream>
struct list
{
int a,b;
};
std::map<int,list> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}