Random Post: God Mode In Windows 7
RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    How To Convert a Number To std::string

    August 29th, 2008

    There are many methods available. You can use itoa, ltoa etc. to convert numbers to char*. However the following method using stringstream can convert any type of number to std::string.

    
    
    #include <sstream>
    using namespace std;
    int main()
    {
       int No = 140;  // Or you can use float No = 10.5f
       stringstream StrStream;
       StrStream << No;
       string MyString = StrStream.str();
       return 0;
    }
    


    The resulting MyString contains the value of No as string.

    Click to see how to convert string to number

    Click to see how to convert CString to std::string


    Slicing of Objects in a C++ Vector

    August 28th, 2008

    Have you ever tried inserting derived class objects (not pointers to objects) to a vector of base class objects? Then what happens is the derived class objects are sliced to base class objects. See the following example.

    class CBase
    {
    public:
       int m_BaseVariable;
    };
    
    class CDerived : public CBase
    {
    public:
       int m_DerivedVariable;
    };
    
    int main()
    {
       vector<CBase> BaseVector;
       CDerived MyDerived;
       BaseVector.push_back(MyDerived);
       .............
       return 0;
    }
    
    

    Now if you watch BaseVector in a debug window, you can see that it does not have the variable m_DerivedVariable. It has been sliced to the base class object.

    Of course, the same slicing will happen if you make an assignment

    CBase MyBase = MyDerived;

    However, things will be complicated and more error prone in a vector where you can push both base type objects and derived type objects.

    Be aware of the above pitfall. At least the compiler (VC++ 6.0, I’m not sure about others) is not giving any error or warning.


    Why I Prefer ‘\n’ To std::endl

    August 27th, 2008

    Both serve the same purpose, putting a new line. The only difference is that endl causes flushing of the output buffer everytime it is called where as ‘\n’ does not.

    So, if you are writing al the alphabets of English to a file using the following code,

    #include <iostream>;
    #include <fstream>;
    using namespace std;
    int main()
    {
       ofstream MyFileStream("Alphabets.txt", ios:: out);
       for (char Index = 'A'; Index <= 'Z'; ++Index)
       {
          MyFileStream << Index << endl;
       }
       return 0;
    }
    

    the output buffer will be flushed 26 times while the same code with ‘\n’ instead of endl will cause it to be flushed only once and that is when the program exits (assuming that the output buffer is larger than 26 bytes).

    Some Background Info

    1. Anything to be output is first queued into an output buffer and written to the device (hard disk, monitor etc…) when the queue is full. Of course, this is to ensure speed of execution as the access to the external device is less frequent.
    2. If you are in a situation where you have to avoid buffering, you can use std::endl instead of ‘\n’. An example of the above situation is when you are using old style debugging using multiple cout statements between code lines  to see where exactly a crash occurs.