CPA Note

CPA – C++ Certified Associate Programmer

Readings

More books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

Reference

cout manipulator

1
2
3
4
int byte = 255;
cout << "Byte in hex: " << hex << byte;
cout << "Byte in decimal: " << dec << byte;
cout << "Byte in octal: " << oct << byte;
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

using namespace std;
int main(void)
{
int byte = 255;
cout << setbase(16) << byte;
return 0;
}
1
2
3
float x = 2.5, y = 0.0000000025;
cout << fixed << x << " " << y << endl;
cout << scientific << x << " " << y << endl;

String methods

compare

str1.compare(str2)

  • str1.compare(str2) == 0 when str1 == str2

  • str1.compare(str2) > 0 when str1 > str2

  • str1.compare(str2) < 0 when str1 < str2

S.compare(substr_start, substr_length, other_string)

S.compare(substr_start, substr_length, other_string, other_substr_start, other_substr_length)

substr

1
string newstr = oldstr.substr(substring_start_position, length_of_substring)

length, size, capacity, max_size

1
2
3
4
5
6
7
int string_size = S.size();

int string_length = S.length();

int string_capacity = s.capacity();

int string_max_size = s.max_size();
1
TheString.reserve(100);

reserve, resize, clear, empty

1
2
3
4
5
6
7
bool is_empty = TheString.empty();

TheString.resize(50,'?');

TheString.resize(4);

TheString.clear();

find

1
2
3
int where_it_begins = S.find(another_string, start_here);

int where_it_is = S.find(any_character, start_here);
1
2
3
4
int comma = greeting.find(',');
if(comma != string::npos){
//found
};

append, push_back, insert

1
2
3
NewString.append(TheString);
NewString.append(TheString,0,3);
NewString.append(2,'!');
1
TheString.push_back(car);
1
2
string quote = "Whyserious?", anyword = "monsoon";
quote.insert(3,2,' ').insert(4,anyword,3,2); // Why so serious?

assign

1
2
string sky; 
sky.assign(80,'*');

replace

1
2
3
4
string ToDo = "I'll think about that in one hour"; 
string Schedule = "today yesterday tomorrow";

ToDo.replace(22, 12, Schedule, 16, 8); // I'll think about that tomorrow

erase

1
2
3
4
string WhereAreWe = "I've got a feeling we're not in Kansas anymore"; 

WhereAreWe.erase(38, 8).erase(25, 4); // I've got a feeling we're in Kansas
TheString.erase();

swap

1
2
3
4
string Drink = "A martini";
string Needs = "Shaken, not stirred";

Drink.swap(Needs);