Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views29 pages

CPP Basics

Uploaded by

simunobo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views29 pages

CPP Basics

Uploaded by

simunobo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

C++ BASICS RELEVANT TO CSI 228

FARIHA TABASSUM ISLAM


BASICS

 C++ language extends the C programming language with additional features such as
type checking, object-oriented programming, exception handling etc.
 C++ was developed by Bjarne Stroustrup in 1979.
 File extension .cpp

 Why C++ in this course?


 The Standard Template Library (STL) of C++ provides useful codes
 STL is a set of C++ template classes to provide common programming data structures and functions such as
lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators.
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 2
SIMILARITIES WITH C

 Variables, Operators
 struct
 Array
 Function
 Pointer can use the same code as written in c
 Strings
 If, if…else-if statement, switch case, for loop,
while loop, do-while loop, continue statement,
break statement, goto statement
 Recursion

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 3


BASICS

C++ C

for details explanation: https://beginnersbook.com/2017/08/first-cpp-program/

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 4


VARIABLES AND DATA TYPES
Output:
inside first if
 int end

 char
 bool  inside first if
 holds Boolean value true or false  end
 double
 float

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 5


Output:
inside first if
inside third nested if
VARIABLES AND DATA TYPES end

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 6


scanf(), printf() EQUIVALENT

C++ C

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 7


scanf(), printf() EQUIVALENT

which one is
the C++ code?

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 8


IF YOU PREFER scanf(), printf() OVER cin, cout

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 9


STANDARD TEMPLATE LIBRARY (STL)

 STL is a set of C++ template classes to provide common programming data structures and functions such as lists,
stacks, arrays, etc. It is a library of container classes, algorithms, and iterators.

 We are going to use


 Sorting
 vector
 priority queue

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 10


VECTOR

 #include <vector>
 To know more about vectors: https://www.edureka.co/blog/vectors-in-cpp/

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 11


Vector

VECTOR

Input:
11 753 2 8 91
Output:
size: 5
[0] 11
[1] 753
[2] 2
[3] 8
[4] 91
11
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU
753 12
8
91
Vector

#include <bits/stdc++.h>

VECTOR

Input:
11 753 2 8 91
Output:
size: 5
[0] 11
[1] 753
[2] 2
[3] 8
[4] 91
11
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU
753 13
8
91
SORTING

 sort array
 sort vector
 sort structure

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 14


SORT Array

Output:
100 512 6 724 31 14 2 0
SORT ARRAY 0 2 6 14 31 100 512 724

Default order/
Ascending order
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 15
SORT Array

Output: Output:
100 512 6 724 31 14 2 0 100 512 6 724 31 14 2 0
0 2 6 14 31 100 512 724 724 512 100 31 14 6 2 0
SORT ARRAY

Default order/
Ascending order Descending order
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 16
SORT Array of struct
Output:
a:5 b:5
a:1 b:6
SORT ARRAY a:3 b:9
a:3 b:12
a:8 b:16
a:5 b:100

this function is a must for sorting an array of struct

No default order. Order must


be specified by a function
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 17
SORT Array of struct
Output:
a:5 b:100 ratio:20
a:1 b:6 ratio:6
SORT ARRAY a:3 b:12 ratio:4
a:3 b:9 ratio:3
a:8 b:16 ratio:2
a:5 b:5 ratio:1

No default order. Order must


be specified by a function
FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 18
STRING

 In C++, string is an object of std::string class


 It stores a sequence of characters.
 e.g. “hello world”

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 19


Input:
cpp strings
cpp strings
Output:
Hello world
hello universe
11
str1[0]:H str1[6]:w
Hello world.
hello universe. Hello world.
cpp
cpp strings

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 20


Output:
yello hello
str2 did not change!!!

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 21


STRING

 Searching within a string.


 search a character key: str.find(key)
 returns the starting position of key
 returns the constant string::npos if not found
 search a string key: str.find(key)

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 22


Output:
First we: 5
Second we: 8
Third we: 28
Is G there? Yes!
Is Z there? No!

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 23


STRING

 Extracting substrings
 str.substring(“hello”);

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 24


Output:
Thank you very very much
Thank you
Thank you very much

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 25


PRIORITY QUEUE

 A priority queue in c++ is a type of container adapter, which processes only the
highest priority element, i.e. the first element will be the maximum of all elements in
the queue, and elements are in decreasing order.
 priority_queue<int> variableName;

 min queue
 priority_queue <int, vector<int>, greater<int>> q;

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 26


PRIORITY QUEUE

 p.empty()
 p.size()
 p.push(10) // insert 10
 firstly, the element is added to the end of the queue, and simultaneously elements reorder themselves with priority.
It takes value in the parameter.
 p.pop()
 deletes the top element (highest priority) from the priority_queue

 p. top()
 returns the top element (highest priority) from the priority_queue

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 27


Output:
23 5
20 6
55
36
05

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 28


REFERENCES

 https://beginnersbook.com/2017/08/c-plus-plus-tutorial-for-beginners/
 https://www.edureka.co/blog/vectors-in-cpp/
 https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1132/handouts/08-C++-
Strings.pdf
 https://www.mygreatlearning.com/blog/priority-queue-in-cpp/
 https://www.geeksforgeeks.org/stl-priority-queue-for-structure-or-class/

FARIHA TABASSUM ISLAM, LECTURER, DEPT. OF CSE, UIU 29

You might also like