This is a draft cheat sheet. It is a work in progress and is not finished yet.
Header
#include <iostream> |
#include <iomanip> |
#include <string> |
#include <cmath> |
#include <cstdlib> |
#include <fstream> |
#include <sstream> |
#include <vector> |
Function Prototypes
void functionName(const double[], int, int&); |
int anotherName(int scores[][3], int size); |
Function Calls
functionName(temperatures, size, high); |
anotherName(scores, SIZE); |
Problems
Subscript out of bounds Too many or few loops
|
No decimals Multiply by 1.0 or change double
|
error: expected unqualified-id before Use instance instead of type
|
String Manipulation
str.substr(pos, len) |
returns string from pos |
str.find(str1) |
returns location index |
str.find(str1, pos) |
starts at pos then finds |
str.insert(pos, str1) |
inserts str1 at pos |
str.erase(pos, length) |
removes length at pos |
Functions
Random # |
(rand() % (max-min+1))+min |
Power |
pow(base, exp) |
Abs Value Int |
abs(num) |
Abs Value Double |
fabs(num) |
OOP
vector |
.push_back() |
|
.resize() |
fstream |
.open() |
|
.fail() |
inserting filename |
.c_srt() |
|
|
Switch
switch(trigger){
case 0: break;
case 1: break;
default: break;
]
|
Bubble Sort for Array
int temp;
bool sorted = 0;
while(!sorted){
sorted=1;
for (i=1;i<num_elements;i++){
if(arr[i-1]>arr[i]){
temp=arr[i-1];
arr[i-1]=arr[i];
arr[i]=temp;
sorted=0;
}
}
}
|
Insert and Erase / Fix
void fixSentence(string& sentence){
int i = sentence.find(" ");
string firstnoun;
firstnoun = sentence.at(i+1);
if (firstnoun == "a"&&"e"&&"i"&&"o"&&"u"){
sentence.insert(1,"n");
}
else{
int j = sentence.find(" ", i + 1);
int k = sentence.find(" ", j + 1);
int verblength = (k - (j+1));
sentence.erase(j+1, verblength);
sentence.insert(j+1, selectWord(VERBS, NUM));
}
}
|
Extract from string stream
watch = "";
string tempstr;
sin>>day>>month>>year>>clouds>>temp>>wind>>watch;
while (sin>>tempstr){
watch = watch + " " + tempstr;
}
|
|