I have a vector std::vector<StudentGradeInfo> gradebook; objects of the class StudentGradeInfo. The StudentGradeInfo class contains the following member variables:
std::string studentName;
int studentID;
std::string major;
What I need to do is overload the == operator to compare the ID input from the user and compare it to the studentID member variables of the objects in the vector std::vector<StudentGradeInfo> gradebook;.
How would I go about looping through the vector to compare the ID's? It would probably be a non-member function since it is comparing an int variable to a member function of an object, but I do not know how to do this.
I guess you could do it like this if you absolutely have to make an overloaded operator==.
bool operator==(const StudentGradeInfo& sgi, int id) {
return sgi.studentID == id;
}
std::vector<StudentGradeInfo> gradebook;
int id_to_find=1234;
for(auto& sgi : gradebook) {
if(sgi==id_to_find) {
//...
}
}
I suppose you want to loop through the vector to find a studentGrandeInfo-object with a particular ID. You can accomplish this rather easy by using std::find_if together with a lambda-function for the proper comparison. The code could look as follows:
int toFind = 4219; // some ID
auto it = std::find_if(
gradebook.begin(), gradebook.end(),
[&toFind](const StudentGradeInfo& x) { return x.studentID == toFind;});
In your case, as you the studentID seems to serve as a "unique key", I'd use data structure std::map<int, StrudentGradeInfo> instead.
Overloading operator == is - as gsamars pointed out - somehow impractical, as it is meant for comparing two objects of the same type (and not an object with an int). See the following code illustrating this:
struct StudentGradeInfo {
std::string studentName;
int studentID;
bool operator==(const StudentGradeInfo& c) const {
return c.studentID == studentID;
}
};
int main() {
std::vector<StudentGradeInfo> gradebook {
{ "john", 123 },
{ "max", 345 }
};
StudentGradeInfo aNewOne { "sepp", 345 };
auto it = find(gradebook.begin(),gradebook.end(),aNewOne);
if (it == gradebook.end()) {
cout << "345 does not exist" << endl;
} else {
cout << "345 already exists" << endl;
}
}
I'm trying to get my head around encapsulation in C++. I have this program working when everything is in public, but when I move the vector to private, I'm having trouble accessing the vector with my public functions. I've tried using a friend and I can't get it to work. How can I get my addElement function to see the vector?
#include <iostream>
#include <vector>
using namespace std;
class MySet{
private:
vector<int> elements;
public:
MySet();
friend void addElement(int value);
};
MySet::MySet (){
vector <int> elements;
}
void MySet::addElement(int value){
elements.push_back(value);
}
void Print(vector<int>& v) {
vector<int>::iterator it;
for(it = v.begin(); it != v.end(); ++it) {
cout << (*it) << '\n';
}
}
int main(int argc, char *argv[]){
int value;
MySet set;
cout << "Enter your numbers,(enter -1 to end)" << endl;
while(cin){
cin>> value;
if(value==-1)
break;
set.addElement(value);
}
Print(set.elements);
system("PAUSE");
}
Basically you want to make a Print a method instead of trying to access the private member variable. In general trying to access the private variable directly is usually a good sign that it shouldn't be private in the first place or that you need to change your design. In this case changing your design would most likely lead to the best outcome:
using namespace std;
class MySet{
private:
vector<int> elements;
public:
MySet();
friend void addElement(int value);
void print() {
vector<int>::iterator it;
for(it = this->elements.begin(); it != this_.elements.end(); ++it){
cout << (*it) << '\n';
}
}
};
Now you can print it like so:
set.print();
And this avoids the issues with using the private member.
Note that we no longer need to pass in the particular instance of MySet to the print function, this is because the class already has that information it needs via the this pointer.
The general idea is that you store the state as private then give a bunch of public functions for the users of the class the manipulate that state. The users shouldn't manipulate the state directly though, so if you find you are doing this step back and reconsider your design.
Basically I want MyClass that holds a Hashmap that maps Field name(string) to ANY type of
Value.. For this purpose I wrote a separate MyField class that holds the type & value information..
This is what I have so far:
template <typename T>
class MyField {
T m_Value;
int m_Size;
}
struct MyClass {
std::map<string, MyField> fields; //ERROR!!!
}
But as you can see, the map declaration fails because I didn't provide the type parameter for MyField...
So I guess It has to be something like
std::map< string, MyField<int> > fields;
or
std::map< string, MyField<double> > fields;
But obviously this undermines my whole purpose, because the declared map can only hold MyField of a specific type.. I want a map that can hold ANY type of MyField clas..
Is there any way I can achieve this..?
This is plain in C++ 17. Use std::map + std::any + std::any_cast:
#include <map>
#include <string>
#include <any>
int main()
{
std::map<std::string, std::any> notebook;
std::string name{ "Pluto" };
int year = 2015;
notebook["PetName"] = name;
notebook["Born"] = year;
std::string name2 = std::any_cast<std::string>(notebook["PetName"]); // = "Pluto"
int year2 = std::any_cast<int>(notebook["Born"]); // = 2015
}
Blindy's answer is very good (+1), but just to complete the answer: there is another way to do it with no library, by using dynamic inheritance:
class MyFieldInterface
{
int m_Size; // of course use appropriate access level in the real code...
~MyFieldInterface() = default;
}
template <typename T>
class MyField : public MyFieldInterface {
T m_Value;
}
struct MyClass {
std::map<string, MyFieldInterface* > fields;
}
Pros:
it's familiar to any C++ coder
it don't force you to use Boost (in some contexts you are not allowed to);
Cons:
you have to allocate the objects on the heap/free store and use reference semantic instead of value semantic to manipulate them;
public inheritance exposed that way might lead to over-use of dynamic inheritance and a lot of long-term issues related to your types really being too inter-dependent;
a vector of pointers is problematic if it have to own the objects, as you have to manage destruction;
So use boost::any or boost::variant as default if you can, and consider this option only otherwise.
To fix that last cons point you could use smart pointers:
struct MyClass {
std::map<string, std::unique_ptr<MyFieldInterface> > fields; // or shared_ptr<> if you are sharing ownership
}
However there is still a potentially more problematic point:
It forces you to create the objects using new/delete (or make_unique/shared). This mean that the actual objects are created in the free store (the heap) at any location provided by the allocator (mostly the default one). Therefore, going though the list of objects very often is not as fast as it could be because of cache misses.
If you are concerned with performance of looping through this list very often as fast as possible (ignore the following if not), then you'd better use either boost::variant (if you already know all the concrete types you will use) OR use some kind of type-erased polymorphic container.
The idea is that the container would manage arrays of objects of the same type, but that still expose the same interface. That interface can be either a concept (using duck-typing techniques) or a dynamic interface (a base class like in my first example).
The advantage is that the container will keep same-type objects in separate vectors, so going through them is fast. Only going from one type to another is not.
Here is an example (the images are from there): http://bannalia.blogspot.fr/2014/05/fast-polymorphic-collections.html
However, this technique loose it's interest if you need to keep the order in which the objects are inserted.
In any way, there are several solutions possible, which depends a lot on your needs. If you have not enough experience with your case, I suggest using either the simple solution I first explained in my example or boost::any/variant.
As a complement to this answer, I want to point very good blog articles which summarize all C++ type-erasure techniques you could use, with comments and pros/cons:
http://talesofcpp.fusionfenix.com/post-16/episode-nine-erasing-the-concrete
http://akrzemi1.wordpress.com/2013/11/18/type-erasure-part-i/
http://akrzemi1.wordpress.com/2013/12/06/type-erasure-part-ii/
http://akrzemi1.wordpress.com/2013/12/11/type-erasure-part-iii/
http://akrzemi1.wordpress.com/2014/01/13/type-erasure-part-iv/
Use either boost::variant (if you know the types you can store, it provides compile time support) or boost::any (for really any type -- but that's kind of unlikely to be the case).
http://www.boost.org/doc/libs/1_55_0/doc/html/variant/misc.html#variant.versus-any
Edit: I cannot emphasize enough that although rolling your own solution might seem cool, using a complete, proper implementation will save you a lot of headache in the long run. boost::any implements RHS copy constructors (C++11), both safe (typeid()) and unsafe (dumb casts) value retrievals, with const corectness, RHS operands and both pointer and value types.
That's true in general, but even more so for low level, base types you build your entire application on.
class AnyBase
{
public:
virtual ~AnyBase() = 0;
};
inline AnyBase::~AnyBase() {}
template<class T>
class Any : public AnyBase
{
public:
typedef T Type;
explicit Any(const Type& data) : data(data) {}
Any() {}
Type data;
};
std::map<std::string, std::unique_ptr<AnyBase>> anymap;
anymap["number"].reset(new Any<int>(5));
anymap["text"].reset(new Any<std::string>("5"));
// throws std::bad_cast if not really Any<int>
int value = dynamic_cast<Any<int>&>(*anymap["number"]).data;
C++17 has a std::variant type that has facilities for holding different types much better than a union.
For those not on C++17, boost::variant implements this same mechanism.
For those not using boost, https://github.com/mapbox/variant implements a much lighter version of variant for C++11 and C++14 that looks very promising, well documented, lightweight, and has plenty of usage examples.
You could also use a void* and cast the value back to the correct type using reinterpret_cast. Its a technique often used in C in callbacks.
#include <iostream>
#include <unordered_map>
#include <string>
#include <cstdint> // Needed for intptr_t
using namespace std;
enum TypeID {
TYPE_INT,
TYPE_CHAR_PTR,
TYPE_MYFIELD
};
struct MyField {
int typeId;
void * data;
};
int main() {
std::unordered_map<std::string, MyField> map;
MyField anInt = {TYPE_INT, reinterpret_cast<void*>(42) };
char cstr[] = "Jolly good";
MyField aCString = { TYPE_CHAR_PTR, cstr };
MyField aStruct = { TYPE_MYFIELD, &anInt };
map.emplace( "Int", anInt );
map.emplace( "C String", aCString );
map.emplace( "MyField" , aStruct );
int intval = static_cast<int>(reinterpret_cast<intptr_t>(map["Int"].data));
const char *cstr2 = reinterpret_cast<const char *>( map["C String"].data );
MyField* myStruct = reinterpret_cast<MyField*>( map["MyField"].data );
cout << intval << '\n'
<< cstr << '\n'
<< myStruct->typeId << ": " << static_cast<int>(reinterpret_cast<intptr_t>(myStruct->data)) << endl;
}
This is a naive way of doing it. Of course, you can add wrappers to void the some boiler plate code.
#include <iostream>
#include <memory>
#include <map>
#include <vector>
#include <cassert>
struct IObject
{
virtual ~IObject() = default;
};
template<class T>
class Object final : public IObject
{
public:
Object(T t_content) : m_context(t_content){}
~Object() = default;
const T& get() const
{
return m_context;
}
private:
T m_context;
};
struct MyClass
{
std::map<std::string, std::unique_ptr<IObject>> m_fields;
};
int main()
{
MyClass yourClass;
// Content as scalar
yourClass.m_fields["scalar"] = std::make_unique<Object<int>>(35);
// Content as vector
std::vector<double> v{ 3.1, 0.042 };
yourClass.m_fields["vector"] = std::make_unique<Object<std::vector<double>>>(v);
auto scalar = dynamic_cast<Object<int>*>(yourClass.m_fields["scalar"].get())->get();
assert(scalar == 35);
auto vector_ = dynamic_cast<Object<std::vector<double>>*>(yourClass.m_fields["vector"].get())->get();
assert(v == vector_);
return 0;
}
Work in progress. The advantage this method has is that you don't have to cast anything when doing assignment, or any of the features listed below.
As of now it can:
store non-container literal types (const char*, double, int, float, char, bool)
output value for corresponding key with ostream operator
reassign the value of an existing key
add a new key:value pair using the append method only, key cannot be the same, or else you get an error message
add literals of the same type with the + operator
In the code, I have demonstrated in the main function what it can currently do.
/*
This program demonstrates a map of arbitrary literal types implemented in C++17, using any.
*/
#include <vector>
#include <any>
#include <utility>
#include <iostream>
using namespace std;
class ArbMap
{
public:
ArbMap() : vec({}), None("None") {} //default constructor
ArbMap(const vector < pair<any,any> > &x) //parametrized constructor, takes in a vector of pairs
: vec(x), None("None") {}
//our conversion function, this time we pass in a reference
//to a string, which will get updated depending on which
//cast was successful. Trying to return values is ill-advised
//because this function is recursive, so passing a reference
//was the next logical solution
void elem(any &x, string &temp, int num=0 )
{
try
{
switch (num)
{
case 0:
any_cast<int>(x);
temp = "i";
break;
case 1:
any_cast<double>(x);
temp = "d";
break;
case 2:
any_cast<const char*>(x);
temp = "cc";
break;
case 3:
any_cast<char>(x);
temp = "c";
break;
case 4:
any_cast<bool>(x);
temp = "b";
break;
case 5:
any_cast<string>(x);
temp = "s";
break;
}
}
catch(const bad_cast& e)
{
elem(x,temp,++num);
}
}
//returns size of vector of pairs
size_t size()
{
return vec.size();
}
/* Uses linear search to find key, then tries to cast
all the elements into the appropriate type. */
any& operator[](any key)
{
ArbMap temp;
string stemp;
for (size_t i = 0; i<vec.size(); ++i)
{
temp.elem(vec[i].first,stemp);
if (stemp=="i")
{
try
{
any_cast<int>(key);
}
catch(const bad_cast& e)
{
continue;
}
if (any_cast<int>(key)==any_cast<int>(vec[i].first))
{
return vec[i].second;
}
}
else if (stemp=="d")
{
try
{
any_cast<double>(key);
}
catch(const bad_cast& e)
{
continue;
}
if (any_cast<double>(key)==any_cast<double>(vec[i].first))
{
return vec[i].second;
}
}
else if (stemp=="cc")
{
try
{
any_cast<const char*>(key);
}
catch(const bad_cast& e)
{
continue;
}
if (any_cast<const char*>(key)==any_cast<const char*>(vec[i].first))
{
return vec[i].second;
}
}
else if (stemp=="c")
{
try
{
any_cast<char>(key);
}
catch(const bad_cast& e)
{
continue;
}
if (any_cast<char>(key)==any_cast<char>(vec[i].first))
{
return vec[i].second;
}
}
else if (stemp=="b")
{
try
{
any_cast<bool>(key);
}
catch(const bad_cast& e)
{
continue;
}
if (any_cast<bool>(key)==any_cast<bool>(vec[i].first))
{
return vec[i].second;
}
}
}
//vec.push_back({key,None});
throw -1;
//return None;
}
void print();
void append(any key, any value);
private:
vector < pair<any,any> > vec;
any None;
};
ostream& operator<<(ostream& out, any a)
{
ArbMap temp; //should be updated to be a smart pointer?
string stemp;
temp.elem(a,stemp); //stemp will get updated in the elem function
//The "if else-if ladder" for casting types
if (stemp=="i") out << any_cast<int>(a);
else if (stemp=="d") out << any_cast<double>(a);
else if (stemp=="cc") out << any_cast<const char*>(a);
else if (stemp=="c") out << any_cast<char>(a);
else if (stemp=="b")
{
if (any_cast<bool>(a)==1)
out << "true";
else
out << "false";
}
else if (stemp=="s") out << any_cast<string>(a);
return out;
}
any operator+(any val1, any val2)
{
ArbMap temp;
string stemp1, stemp2;
temp.elem(val1,stemp1);
temp.elem(val2,stemp2);
try
{
if (stemp1 != stemp2)
throw -1;
if (stemp1 == "i")
{
return any_cast<int>(val1)+any_cast<int>(val2);
}
else if (stemp1 == "d")
{
return any_cast<double>(val1)+any_cast<double>(val2);
}
else if (stemp1 == "cc")
{
return string(any_cast<const char*>(val1))+string(any_cast<const char*>(val2));
}
else if (stemp1 == "c")
{
return string{any_cast<char>(val1)}+string{any_cast<char>(val2)};
}
else if (stemp1=="b")
{
return static_cast<bool>(any_cast<bool>(val1)+any_cast<bool>(val2));
}
}
catch (int err)
{
cout << "Bad cast! Operands must be of the same 'type'.\n";
}
return val1;
}
void ArbMap::print()
{
cout << '\n';
for (size_t i = 0; i<vec.size(); ++i)
{
cout << vec[i].first << ": " << vec[i].second << '\n';
}
cout << '\n';
}
void ArbMap::append(any key, any value)
{
try
{
(*this)[key];
throw "Already exists!";
}
catch(int error)
{
vec.push_back({key,value});
}
catch(const char* error)
{
cout << "ArbMap::append failed, key already exists!\n";
}
}
int main() {
ArbMap s({{1,2},{"aaa",1.2},{'c',33.3},{"what","this is awesome"}, {true, false}});
cout << s[1] << '\n' << s["aaa"] << '\n' << s['c']
<< '\n' << s["what"] << '\n'
//Uncomment the line below and runtime error will occur, as
//entry is not in the dictionary
// << s["not in the dictionary bro"] << '\n'
<< s[true] << '\n';
s.print();
s[1] = "hello";
s.print();
s.append(2.3,"what");
s.print();
s[2.3] = "hello";
s.print();
s.append(2.3,"what");
s.print();
s[1] = 1.2;
s.print();
s.append(2.4,1.2);
//Operator +
cout << s[1]+s[2.4] << '\n';
cout << s["what"] + s[2.3] << '\n';
s.append('d','a');
cout << s['c'] << '\n';
cout << s[2.4]+ s["aaa"]+ s['c'] + s['c'] + s['c'] << '\n';
cout << s[true]+s[true] << '\n';
return 0;
}
I have a class which made of a ints and strings, but I also have a vector inside that class. I have to read the records from a file and then after parsing each line, put the info in my vector of class. I have to get the basic package information like ID and name and then add the services which are offered with that package, so I can have 10 records that are from one package but they are different in type of services. For now I am trying to work out putting the data in each package and access the data from each element, but when I am trying to get the data from the vector inside the class, my compiled file crashes. It also prints out the 1233 and foo, but not the test. Any ideas why is that?
int main()
{
vector<package> packs;
package pack;
pack.ID = 1233;
pack.name = "foo";
packs.push_back(pack);
pack.putData("test",12);
cout << packs[0].name << endl;
cout << packs[0].ID << endl;
cout << packs[0].bservice[0].serviceID << endl; //[b]Crashes in this line[/b]
return 0;
}
Defined class is:
class package
{
public:
class aservice
{
public:
int serviceID;
string othername;
};
int ID;
string name;
vector<aservice> bservice;
void putData(string name1, int serviceID1)
{
aservice obj;
obj.serviceID = serviceID1;
obj.othername = name1;
bservice.push_back(obj);
}
};
Here you make a copy of pack when you push_back into the vector:
packs.push_back(pack);
And here you access pack, not the copy stored in your vector
pack.putData("test",12);
So the bservice vector you are trying to access is actually empty, which is why your code crashes when you try to access it here:
cout << patients[0].bservice[0].serviceID << endl; // patients[0].bservice is empty!!!
You can avoid this by pushing back after the call to putData:
vector<package> packs;
package pack;
pack.ID = 1233;
pack.name = "foo";
pack.putData("test",12);
packs.push_back(pack);
You can also avoid it by not trying to access a vector without first checking whether it is empty.
Ideally you should strive to design classes than can be constructed into a useful state, as opposed to default constructing them and adding data step by step via setters. This is particularly important if the data are inter-related and the class must maintain invariants.
packs.push_back(pack);
Is going to push a copy of pack into your vector. You will therefore have two specific instances : if you call putData on one of those, the other one will not be modified itself !
Therefore, when writing
patients[0].bservice[0]
your application crashes because you did not putData inside patients[0], only inside pack - which is, once again, a different object.
You should modify your vector so it stores pointers to package's, and push the adress of pack inside.
pack.push_back(pack);
Assuming the first pack is actually packs, this pushes a copy of pack onto the vector.
pack.putData("test",12);
This modifies the local variable pack, but not the copy you pushed onto the vector. That still contains an empty bservice vector.
cout << patients[0].bservice[0].serviceID << endl;
Assuming that patients is actually packs, this erroneously attempts to read from the empty bservice vector.
You either want to call putData before packs.push_back(pack), or call it on packs.back() rather than the local pack.
Try this:
#include <vector>
#include <iostream>
using namespace std;
class package
{
public:
package(int inID, const string& inName ) : ID(inID), name(inName)
{
}
void putData(string name1, int serviceID1)
{
aservice obj;
obj.serviceID = serviceID1;
obj.othername = name1;
bservice.push_back(obj);
}
void Print() const
{
cout << ID << endl;
cout << name << endl;
vector<aservice>::const_iterator iter;
iter = bservice.begin();
for (; iter != bservice.end(); ++iter)
{
cout << iter->serviceID << " " << iter->othername << endl;
}
}
private:
class aservice
{
public:
aservice() {};
int serviceID;
string othername;
};
int ID;
string name;
vector<aservice> bservice;
};
typedef vector<package> PackContainer;
typedef vector<package>::iterator PackContainerIterator;
typedef vector<package>::const_iterator PackContainerConstIterator;
void PrintAll(const PackContainer& packs)
{
PackContainerConstIterator iter = packs.begin();
for (; iter != packs.end(); ++iter)
{
iter->Print();
}
}
int main()
{
PackContainer packs;
package pack( 1233, "foo");
pack.putData("test",12);
packs.push_back(pack);
PrintAll(packs);
return 0;
}
I have a parent class which holds a map and n the child class i have used to inherit that class with for some reason can't access the map which i can't under stand why, i want to access the values inside the map.
my code is as follows
//HEADER FILE
#include <iostream>
#include <map>
using namespace std;
//////PARENT CLASS
struct TTYElementBase
{
//some code here
};
class element
{
public:
std::map<char,std::string> transMask;
std::map<char,std::string>::iterator it;
void populate();
};
//////CHILD CLASS .HPP
class elementV : public element
{
public :
std::string s1;
std::string s2;
elementV();
friend ostream &operator<< (ostream &, const elementV &);
void transLateMask();
};
//CPP FILE
#include "example.h"
#include <iostream>
elementV::elementV()
{
}
void elementV::transLateMask()
{
for ( it=transMask.begin() ; it != transMask.end(); it++ )
cout << (*it).first << endl;
}
int main()
{
elementV v;
v.transLateMask();
}
// ' OUTPUT IS NOTHING I DONT KNOW WHY?'
output is nothing but i need to acces the map fron the parent class, what am i doing wrong?
any help i will be very gratefull
Thanks
Does the map contain an entry for 'D' when you call transLateMask()? You'll get undefined behaviour (perhaps a runtime error) if it doesn't, since you don't check the result of find(). Something like this would be more robust:
auto found = transMask.find('D');
if (found == transMask.end()) {
// handle the error in some way, perhaps
throw std::runtime_error("No D in transMask");
}
std::string str = found->second;
(If you're not using C++11, then replace auto with the full type name, std::map<char,std::string>::const_iterator).
Alternatively, C++11 adds an at() method which throws std::out_of_range if the key is not found:
std::string str = transMask.at('D')->second;
The find() method of the std::map can return an iterator that is "one beyond the end" of the map, i.e. equals to result of end(). This means there's no such entry in the map. You have to check for that:
typedef std::map<char,std::string> mymap;
mymap::const_iterator i = transMask.find('D');
if ( i == transMask.end()) {
std::cerr << "'D' not found" << std::endl;
} else {
...
}