$\require{cancel}$
Data Types in C++, Intro and Ints part 1
- C++ supports multiple types of data.
- Simple data types are types which can not be broken down any further.
- Structured data types are types which are composed of one or more data type.
- The simple types include
- Integer types
- Floating Point Types
- Enumerated types
- The address types will be discussed later.
- The structured types include
- Arrays a collection of homogeneous data.
- Structures a collection of (potentially) heterogeneous data.
- Classes a collection of data and functions.
- But what about strings?
- Strings are actually an example of a class.
- The data is probably an array of characters
- But they have associated functions
- Let's take a quick look at cppreference.com string page.
- With strings you can
- Ask for the size (.size() or .length())
- Do all forms of comparison
- find substrings
- extract substrings
- And MANY other things.
- We will be back to take a closer look at strings later.
- Integers
- These are whole numbers.
- They always include 0
- But they can include negative numbers.
- The basic type is
int
- This is usually a 32 bit number, but
- It could be as small as 16 bits
- And it could be larger.
- A 32 bit integer is in the range $-2^{31}$ to $2^{31}-1$
- In general with n bits
- The range is $-2^{n-1}$ to $2^{n-1}-1$
- Since this can change, we can ask for the value.
- Old style defined in
<limits.h>
- New style,
-
#include < limits>
-
numeric_limits<type>
- min(), max(), lowest()
- Plus many others.
- See this reference (we will use it again soon)
- Take a look at intSize.cpp
- Memory can be a problem
- Think about a simulation of galaxies colliding.
- I saw an estimate of about 100 billion stars in a galaxy.
- That is 100x10^9, or 100GB of data if we only use one int for each star.
- We will probably need to scale that, but still that is quite a bit of data.
- Could we use a "smaller" number to represent each?
- To deal with this, we have different integer types.
- A
short
is the smallest regular integer type.
- This is really a
short int
but that is not required.
- It is at least 16 bits.
- See this reference.
- A
long
is the largest regular, but ...
- Again, it is a
long int
- It is at least 32 bits.
- But in 11, they introduced a
long long int
or just long long
- Also, if we know we only have positive integers
- We can declare something to be
unsigned
-
unsigned long long int
, or unsigned long long
-
unsigned long int
, or unsigned long
-
unsigned int
-
unsigned short int
or unsigned short
- Unsigned shifts the range of an n bit integer to be from 0 to $2^{n}-1$
- Wow that is too much, which do I use?
- In most situations I use an
int
- If I want a really big number I use an
unsigned long long
- Take a look at pi.cpp.