array of chars, uses ‘0’ to end the string
char s[11]; declares c-string, creates space for 10 chars
char s[20] = “Hi There”; declares and initializ c-string char message[] - “abc”; also acceptable char message[] = {‘a’, ‘b’} not acceptable
initializing a c-string does not cause a n to be inserted
string = “hello”; not llegal assignment opperator does not work with c-string
#include<cstring> places Hello and 0 into a_string char a_string[11]; strcpy (a_string, “Hello”); strcopy does not check size of array
can write chars beyond declared size causing error
char message[10]; strncpy(message, strvar, 9); will only copy up to 9 chars
avoiding problem with str cpy *will not add ‘0’ to end
strlen(str) returns number of chars in string strcat(str, “hello”) concats str with “hello”
char str[80] cin.getline(a, 80) reads entire line including spaces
atoi(“1234”) converts to int, if non number char - returns 0 atol converts to long atof converts to double
abs(int) labs(long)
standard string class
getline(cin, line) entered text assigned to string line cin.get reads one char at a time getline(cin str, ‘?’) stops at ‘?’ instead of ‘n’ cin.ignore(1000, ‘n’) reads upto 1000 chars or to ‘n’ str.length() returns length of string
comparison operators compare the lexicographic order of strings
vectors (like java arrayList)
vector<int> v; creates vector of ints v[i] = 42; can change value at i but not initialize value at i
initializing a value this way will not show an error but will cause problems in the program.
v.resize(24) resizes vector to 24, elements after are lost
pointer is memory address of a variable pointer variables must have a type
double *p; declares a pointer to a type double
& operator used to get address of variable * derefrencing operator
*p gets the variable pointed to by p
p1 = new int; //variable can be referred to as *p1
(*head).count = 12; AND head -> count = 12; ARE THE SAME
};
typedef ListNode* ListNodePtr; (*head).count = 12; //head is pointer, *head is the node head points to
dot operator has higher precedence than the dereference operator*, so parentheses are required
head -> count = 12; //does the same as (*head).count=12;
ListNodePtr head; //head points to the first/only node head = new ListNode
cout << “Hello”; output to console cin >> var1; input from consol cout << num << “is cool” output mulitple values cout << (price + tax); output arithmetic expressions << insertion operator
cout.precision(5); number of digits after the decimal point
float output/magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
static_cast<double>(9) converts 9 to 9.0
#include<iostream> #include<fstream> ifstream fin; fin.open(“input.txt”); fin.close() ofstream fout; fout.open(“output.txt”); fout.close()
}
new-line n horizonal tab t alert a backslash \ double quote ” endl same as n but should not be in quotes/strings
a collection of names such as cin and cout each name in a namespace has one specification in the namespace
short int long float double long double
const type_name variable_name;
List of constants of type int enum Month {JAN, Feb} //JAN value will be set to 0, FEB will be set to 1
}
sqrt(number) pow(num, power) fabs(double) absolute value ceil(double) round up floor(double) round down