What is a data structure?
Examples?
bool
, char
, short
, int
, etc.What is an algorithm?
Examples?
Two types of complexity
int
s vs array of int
sBig-O notation
We can subdivide complexity into three cases:
Example: linear search time complexity:
int linear_search (int A[], int N, int searchValue) {
int i;
for (i = 0; i < N; ++i) {
if (A[i] == searchValue) {
break;
}
}
return i;
}
#include <iostream>
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
linear_search (a, 10, 0)
10
linear_search (a, 10, 1)
0
linear_search (a, 10, 5)
4
linear_search (a, 10, 10)
9
Languages include data structures/ADTs/algorithms
ArrayList
, TreeSet
)The C++ Standard Container Library provides:
array
, vector
, deque
, list
, forward_list
set
, multiset
, map
, multimap
unordered_set
, unordered_multiset
, unordered_map
, unordered_multimap
stack
, queue
, priority_queue
string
, valarray
, bitset
(container-like)accumulate
copy
sort
lower_bound
, upper_bound
nth_element
partition
#include <vector>
std::vector<int> v { 7, 4, 9, 3, 1};
for (size_t i = 0; i < v.size(); ++i) {
std::cout << v[i] << " ";
}
7 4 9 3 1
v.push_back(2);
v
{ 7, 4, 9, 3, 1, 2 }
v.insert (v.begin(), 5);
v
{ 5, 7, 4, 9, 3, 1, 2 }
v = {7, 4, 9, 1, 3 }
{ 7, 4, 9, 1, 3 }
v.resize(8); // grow to 8 elements
v
{ 7, 4, 9, 1, 3, 0, 0, 0 }
v.resize(3); // shrink to 3 elements
v
{ 7, 4, 9 }
v.shrink_to_fit();
std::cout << "Size: " << v.size() << '\n';
std::cout << "Capacity: " << v.capacity() << '\n';
v
Size: 3 Capacity: 3
{ 7, 4, 9 }
v.insert(v.begin(), 10);
std::cout << "Size: " << v.size() << '\n';
std::cout << "Capacity: " << v.capacity() << '\n';
v
Size: 4 Capacity: 6
{ 10, 7, 4, 9 }
v.erase(v.begin());
std::cout << "Size: " << v.size() << '\n';
std::cout << "Capacity: " << v.capacity() << '\n';
v
Size: 3 Capacity: 6
{ 7, 4, 9 }
a.k.a. Container Adapters
std::stack
)<stack>
std::queue
)<queue>
std::priority_queue
)<priority_queue>
std::set
, std::map
, std::multiset
, std::multimap
<set>
and <map>
Question: Maximum number of comparisons for search?
int
s, Person
s, List
s)ADT Operations Description
operationName
-- action statement specifies:time24
¶Suppose we have a time24 ADT that has a duration
method
duration$(t)$
time24
time24
valuetime24 now {14, 45};
time24 later {16, 25};
now.duration (later)
now = {14, 45};
later = {12, 15};
now.duration (later)
struct time24 {
int hour;
int minute;
time24 duration(time24 other) const {
// assert ((other.minute + 60 * other.hour) >= (minute + 60 * hour))
if (minute > other.minute) {
--other.hour;
other.minute += 60;
}
other.hour -= hour;
other.minute -= minute;
return other;
}
};
class MyClass
{
public:
// public member functions and (maybe) data members
// Form an interface -- Choose wisely!
private:
// implementation details go here!
// - private data members
// - private helper functions
};
public
section visible to clientsprivate
section visible to member functions only (and friends)class C
{
public:
// methods with access to 'field'
private:
int field;
};
C c;
c.field = 3;
input_line_18:10:3: error: 'field' is a private member of 'C' c.field = 3; ^ input_line_18:6:9: note: declared private here int field; ^
Interpreter Error:
.h
or .hpp
(e.g. MyClass.h
).cpp
(e.g. MyClass.cpp
)MyClass
include MyClass.h
#include "MyClass.h"
::
class Rectangle
{
public:
// This is the constructor. It's a special function
// that allows us to initialize a new instance of
// a Rectangle with a specified length and width
Rectangle (double length = 0.0, double width = 0.0)
// below is a member initializer list. For each
// member variable of our class, we should always
// initialize them according to some reasonable
// default
: m_length (length)
, m_width (width)
{
// in this case, our constructor "body" is empty
}
// area is a const member function (denote the trailing const)
// it offers a read-only view of the current class and calculates
// the area of the rectangle
double
area() const
{
// inside we can access (read) any member variable
return m_length * m_width;
}
double
perimeter() const
{
return 2 * (m_length + m_width);
}
double
getLength() const
{
return m_length;
}
double
getWidth() const
{
return m_width;
}
// mutator method which sets a new length and width
double
setSides (double length, double width) {
m_length = length;
m_width = width;
}
// member variables (usually) go at the end of the class
// definition. They should probably be marked as "private"
// most of the time
private:
double m_length;
double m_width;
};
Application Programming Interface
double frandom();
int random();
int random(unsigned int n);
RandomNumber rndA, rndB (7);
for (int i = 0; i < 5; ++i)
{
// 0 <= item < 40
int item = rndA.random (40);
// 0.0 <= x < 1.0
double x = rndB.frandom ();
std::cout << item << " " << x << '\n';
}
std::string
¶string
API followsfind_first_of
find_last_of
find
substr
insert
erase
string::npos
(static)operator+()
to concatenate