implementing map<mpz_t, double> in c++ - c++

For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:

I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}

Related

C++ unique_ptr and arrays

I'm trying to use arrays with unique_ptr with no success.
What is the correct way to declare a unique_ptr of some size?
(size is some paramter).
unique_ptr<A[]> ptr = make_unique<A[]>(size);
Here's an example:
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}
This is not working, however, if I delete the constructor of A, it works.
I want the 3 to represent the size of the array, and not an argument to A's constructor, how do I make that happen?
This is not working, however, if I delete the constructor of A, it
works.
When you removed the user defined constructor, the compiler implicitly generates a default one. When you provide a user defined constructor, the compiler doesn't implicitly generate a default constructor.
std::make_unique<T[]> requires the use of default constructors...
So, provide one, and all should work well
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A() = default;
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}

c++ Class Members Returning Odd values

I will start this out by saying c++ is my first programming language and I am a beginner at best. And I am sure this has some sort of obvious answer.
But for some reason this simple program with a single custom class is returning very odd values from the Get functions that are calling the private members of the one custom class.
The program is separated into three file as follows.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
using namespace std;
int main ()
{
double test=20.0;
Cube D(test);
cout<< D.GetSA()<<endl<<D.GetSide();
return 0;
}
then as header files for the one Classes called Shapes.h
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
using namespace std;
class Cube
{
public:
Cube();
Cube(double);
double GetSA() const;
double GetSide() const;
private:
double SA;
double V;
double Side;
};
And another file Called Shapes.cpp that contains the Constructor.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
Cube::Cube()
{
V=0.0;
SA=0.0;
Side=0.0
}
Cube::Cube(double Side2)
{
Side=Side2;
}
double Cube::GetSA() const
{
return SA;
}
double Cube::GetSide() const
{
return Side;
}
for some reason when this program is run it returns a value of 6.95293e-310 for the GetSA accessor function and returns a value of 200 for the side function.
Any ideas on why this is happening and how to fix it?
Try invoking the default constructor. It seems to initialize the data members correctly. Your parameterized constructor is only initializing the Side data member, and not any other data members.

Trouble returning an object from a class I created

I have this class (hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
And a function header file (functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
And the main file:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
I created the hashMap class and I want to be able to return a hashMap object, but this is giving me an error of "Error C4430 missing type specifier - int assumed." What am I doing wrong?
I put the code in files and nicely asked the compiler to do its job. This is the first warning from the list:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
The compiler doesn't know what hashMap is. When it reaches the line with the error, the hashMap symbol was not yet declared or defined.
You shouldn't define functions in header files.
Rename functions.h to functions.cpp, add #include "functions.h" at the end of the list of includes.
Create a new file functions.h that contains only the declarations of the functions (the function header) and the types they use:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
You are including functions.h from hashmap.h BEFORE the hashMap class is defined. As such, when the compiler reads functions.h, the hashMap class is not defined.

Enforce constness using boost::adaptors::indirected

I'm using boost 1.52 and Visual Studio 2010.
I'm wondering why I can't enforce on the elements returned by boost::adaptors::indirected.
This sample code shows what I'm trying to achieve:
#include <vector>
#include <string>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/foreach.hpp>
int main() {
using std::vector;
using std::string;
typedef vector<string*> type;
type range;
range.push_back(new string("42"));
type const& const_range = range;
// This is ok to compile
BOOST_FOREACH(string const& foo, const_range | boost::adaptors::indirected) {
//...
}
// I don't want this to compile
BOOST_FOREACH(string& foo, const_range | boost::adaptors::indirected) {
//...
}
return 0;
}

map in header file c++

Simple question....
I have a header file called bag...
#include <map>
#include <iostream>
#include <string>
using namespace std;
class Bag
{
public:
Bag();
map <const string str, int number> items;
private:
};
#endif
In the implementation, I'd like to insert something into bag:
#include <string>
#include <fstream>
#include <map>
#include "Bag.h"
using namespace std;
Bag::Bag()
{
items["brush"] = 4;
}
But for some reason, i can't access items. What am I doing wrong????
Thanks!
Duh! For some reason I'm trying to insert a value name with the value type field. Thanks Chad, I think you fixed me!
map <const string, int> items;