Wednesday, September 18, 2013

Vectors

Vectors look very useful - basically seem to be arrays you can extend. C++ reference is here. This is an excellent tutorial - part 1. A few basic points:
  • Indexing starts at zero and is done with [] brackets e.g. my_list[0]
  • Looks like there are some methods taking you to the beginning and end of the list
  • .set(value) one method to put stuff in an array
  • .push_back(value) adds on to the end so may be more useful
  • Can access with pointers as well as with []
  • Can be used with iterators

Sunday, September 15, 2013

Data types and their maximum values

The code below will give you the maximum values of items under c++11. Note you need a long long for Project Euler problem 3 Cpp reference also gives the types and the expression. However I don't really understand the syntax some_phrase::function() etc - remember to look out for this. Looks like there are many numeric_limits that can be accessed beyond max.
#include <iostream>
#include <limits>


using namespace std;

int main()
{
    cout << numeric_limits<unsigned long long>::max();
    return 0;
}

Tuesday, September 10, 2013

Excellent books list for c++

Great advice from this book list...

Converting data types - for example float to int

C++ produces warnings etc if you try and convert between data types - e.g. in something like int target = sqrt(number). However you can flag specific conversions to the compiler in the language.

This article explains why the preferred approach is to use is static_cast.

The explanation is rather obscure but seem to be related to the fact that what you are really doing is converting between different classes and while this is easy to understand between float and int for example could get more complicated at other times and there are some casting approaches that use some error checking - e.g. dynamic_cast. Well - at least that's my guess given the obscurity of the whole thing.

Syntax is here - with pointers to make things difficult:

MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);

Sunday, September 8, 2013

Converting between strings and numbers

Something that should be easy seems to be more difficult than expected. Below as some conversion code not fully understood at the time of writing - i.e. have to understand the difference between ostringstream and stringstream.
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{

    int x = 1234;
    int y;
    string result;

    // converts integer to string which is held in result
    ostringstream conv;
    conv << x * 2;
    conv << 000; //gives a funny result - adds on a 0 at the end of the stream because we are only adding on zero as an integer, but after conversion....
    conv << "00"; //this does padd the result with two zeros - i.e similar to multiplying by 100
    cout << conv.str() << " converts number to string" << endl;
    result = conv.str();
    cout << result << " converted integer assigned to string variable" << endl;


    //takes the string created above and converts back to number
    stringstream string_to_num; // I tried this with conv
    string_to_num << result;

    string_to_num >> y;
    cout << y / 2000 << " output y and do numerical operations on y gets you back to x" << endl;

    return 0;
}