Tuesday, 26 July 2016

C++ overload operators

Three cases are enlisted below as templates -- . More could be added later...

(1) Box1 + Box2

// Overload + operator to add two Box objects.
Box operator+(const Box& b) 
{ 
    Box box;      // this is the new box to return
    box.length = this->length + b.length; 
    box.breadth = this->breadth + b.breadth; 
    box.height = this->height + b.height; 
    return box; 
}

(2) Box1 <  Box2 // Overload < operator to add two Box objects.
        bool operator<(Box &b)
        {
            if(this->l < b.l)
                {return true;}
            else if(this->b < b.b && this->l == b.l)
                {return true;}
            else if(this->h < b.h && this->b == b.b && this->l == b.l)
                {return true;}
            else
                {return false;}
        }



(3) Overloading the I/O operators

i) the output <<
//Overload operator << as specified
        friend ostream& operator<< (ostream &out, const Box &b)
        {
            cout<<b.l<<" "<<b.b<<" "<<b.h;
            return out;
        }


The above examples should be included as the public functions in the Class Box.

ref:

Monday, 25 July 2016

C++ Extract multiple int from one string using stringstream

Sometimes you might get the input such as "23,4,56" and you would like to extract them as integers. There needed some kind of iteration through the string. However, we do not know the length of how many numbers are there in the string.

I write the following case for this function.

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
    vector<int> vec;
    string input = "23,4,56";
    stringstream ss;
    ss << input;
    string temp;
    int iTemp;

    while(std::getline(ss, temp,',')) {
        if(std::stringstream(temp)>>iTemp)
        {
            vec.push_back(iTemp);
        }
    }
    
    for(int i=0;i<vec.size();i++)
    {
        cout<<vec[i]<<endl;
    }
    
    return 0;
}

ref:
http://stackoverflow.com/questions/21594533/c-extract-int-from-string-using-stringstream

c++ Static variable for object count


It is very often that you might need to count the current number of instance of this class. 


class Widget {
public:
    Widget() { ++count; }
    Widget(const Widget&) { ++count; }
    ~Widget() { --count; }
    static size_t howMany()
    { return count; }
private:
    static size_t count;
};

size_t Widget::count = 0;


Intuitively, you need a static variable in the class. However, it is very important to remember that 
static size_t count; is only a declaration and there is no memory allocated and referred to this variable. That is, we need the last line, size_t Widget::count = 0;, so that the linker knows what memory location you're referring to when you use the name.


ref:
http://www.drdobbs.com/cpp/counting-objects-in-c/184403484
http://stackoverflow.com/questions/7781188/static-variable-for-object-count-in-c-classes

C++ String Integer Conversion


There are several methods but I prefer this way.

1
2
3
4
5
6
7
template <typename T>
  string NumberToString ( T Number )
  {
     ostringstream ss;
     ss << Number;
     return ss.str();
  }
Usage: NumberToString ( Number ); 

1
2
3
4
5
6
7
template <typename T>
  T StringToNumber ( const string &Text )
  {
     istringstream ss(Text);
     T result;
     return ss >> result ? result : 0;
  }
Usage: StringToNumber<Type> ( String ); 

But #include <sstream> must be added at the top.


ref:
http://www.cplusplus.com/articles/D9j2Nwbp/