COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Unsigned integers




(page 2)


sizeof




(page 3)


Standard sizes




(page 4)


Static casting


double d;
d = static_cast(2.0/3.0) + 0.2;


(page 5)


Mathematical functions


g++ -Wall -g -o hello hello.cc -lm


(page 6)


std::string




(page 7)


Constructing Strings




(page 8)


Accessing Elements




(page 9)


Converting to C-style strings


#include
std::string s = "52241";
long l = atol(s.c_str());


(page 10)


Comparisons




(page 11)


Substrings


std::string s = "hello, how are you?";
std::cout << s.substr(7, 3) << '\n';


(page 12)


Append and Concatenate




(page 13)


Insertion


std::string s1 = "abcghi";
std::string s2 = "def";
s1.insert(3, s2);
s1.insert(5, 2, '*');
std::cout << s1; // output abcde**fghi


(page 14)


Searching


std::string s1 = "abcdef---defghi";
std::string s2 = "def";
s1.find(s2); // returns 3
s1.rfind(s2); // returns 9



(page 15)


Searching


std::string s1 = "abcdef---defghi";
std::string s2 = "gec";
s1.find_first_of(s2); // returns 2
s1.find_last_of(s2); // returns 12



(page 16)


Searching


std::string s1 = "abcdef---defghi";
std::string s2 = "ghiabc";
s1.find_first_of(s2); // returns 3
s1.find_last_of(s2); // returns 11


(page 17)


Replace



(page 18)


std::vector


std::vector vi;
std::vector vc;
vi.push_back(1); vi.push_back(10);
vc.push_back('a'); vc.push_back('z');
std::cout << vi.at(1) << vc[0] << '\n';


(page 19)


Constructing Vectors




(page 20)


Accessing Elements




(page 21)


Special types





(page 22)


Special types usage


#include
#include
int main() {
std::string s;
std::cin >> s;
std::string::size_type a = s.find('a');
std::string::size_type e = s.find('e');
std::string::difference_type d = e - a;
std::cout << d << '\n';
}


(page 23)


Typedefs



typedef std::string::size_type str_sz;
typedef std::string::difference_type str_df;
str_sz a = s.find('a');
str_sz e = s.find('e');
str_df d = e - a;


(page 24)


No Match?




(page 25)


Other data structures




(page 26)


std::stack


#include
int main() {
std::stack s;
for (int i = 0; i < 10; i++)
s.push(i);
while ( ! s.empty()) {
std::cout << s.top() << stl::endl;
s.pop();
}
}
  • Also has s.size()


(page 27)


Pass by Value


int sum(std::vector v) {
int result = 0;
for (std::vector::size_type i = 0;
i != v.size(); ++i)
result += v[i];
return result;
}
  • Arguments can be safely modified
  • Inefficient due to copying

(page 28)


Pass by Value


void pass_by_value(int i) {
i = 4;
}

int main() {
int j = 1;
pass_by_value(j);
std::cout << j << std::endl;
// outputs 1, not 4
}

(page 29)


Pass by Reference


int sum(std::vector &v) {
int result = 0;
for (std::vector::size_type i = 0;
i != v.size(); ++i)
result += v[i];
return result;
}
  • Modifying argument also modifies the original

(page 30)


Pass by Reference


void pass_by_ref(int &i) {
i = 4;
}

int main() {
int j = 1;
pass_by_ref(j);
std::cout << j << std::endl;
// outputs 4, not 1
}

(page 31)


Pass by Const Reference


int sum(const std::vector &v) {
int result = 0;
for (std::vector::size_type i = 0;
i != v.size(); ++i)
result += v[i];
return result;
}


(page 32)


Pass by Const Reference


void pass_by_const_ref(const int &i) {
// compilation error on following line
i = 4;
}

int main() {
int j = 1;
pass_by_const_ref(j);
std::cout << j << std::endl;
}



















(page 33)


A Simple Example




(page 34)


#include
#include
#include
#include

// width in characters of histogram bars
const int bar_width = 20;

// the count of marks in each decile
std::vector marks(10, 0);


(page 35)



// find maximum in vector
int find_max(const std::vector &v) {
int max = 0;
for (int i = 0 ; i < v.size() ; ++i)
if (max < v[i])
max = v[i];
return max;
}


(page 36)



// read in the marks
int mark;
while (std::cin >> mark) {
int decile = mark / 10;
if (decile > 9)
decile = 9;
marks[decile]++;
}

// get maximum decile count
max_len = find_max(marks);


(page 37)



// output the histogram
for (int i = 0; i < 10 ; ++i) {
std::cout << std::setw(2) << i*10;
std::cout << '-' << std::setw(2);
if (i != 9)
std::cout << i*10 + 9;
else
std::cout << "00";
std::cout << ' ' << std::string(
marks[i] * bar_width / max_len,
'#') << std::endl;
}


(page 38)


Sample Output


0- 9 ##########
10-19 #####
20-29 #####
30-39 ####
40-49 ########
50-59 ##########
60-69 ##############
70-79 ####################
80-89 ####################
90-00 ###############



(page 39)