Lecture 24
Lecture 24
Puru
with
CS101 TAs and Staff
class Queue{
int elements[N], nWaiting, front;
public:
Queue(){…}
bool remove(int &v){…}
bool insert(int v){…}
};
Chapter 22
• Now you can read from that file by invoking the >> operator!
ofstream outfile(“f2.txt”);
// constructor call. Object outfile is created and associated
// with f2.txt, which will get created in the current directory
repeat(10){
int v;
infile >> v;
outfile << v;
}
// f1.txt must begin with 10 numbers. These will be read and
// written to file f2.txt
}
• >> , <<, !
• put, write
str[5] = ‘\0’;
cout << str; // back to earth!
• Not objects!
CS101 Autumn 2019 @ CSE IIT Bombay 13
the string class
string str = “earth”;
int ans = 0;
for (int nx=0; nx < needles.size(); ++nx) {
char ch = needles[nx];
for (int hx = 0; hx < haystack.size(); ++hx) {
if (ch == haystack[hx]) {
++ans;
break; // quit on first match
}
} // ends haystack loop Generalize to work in
} // ends needles loop case needles can also
}
have repeated
characters
CS101 Autumn 2019 @ CSE IIT Bombay 18
Duplicate needles
• Two approaches
– Dedup needles before executing earlier code (reducing
to known problem)
– Dedup needles “on the fly” (inside the nx loop)
However, we did not return (delete) that memory before destroying the
address. Heap allocation functions think that it has been given to us.
void scopy(char*
// We return a reference to the class to ptr, char* rhs){
for(int i=0; i<len(rhs);i++) {
// allow chaining of assignments. ptr[i] = rhs[i];
return *this; }
} }
return *this;
}
– we know that the destination object is also just being created, and
hence its ptr cannot be pointing to any heap variable.
int main(){
mystring names[100];
char buffer[80]
for(int i=0; i<100; i++){
cin.getline(buffer,80);
names[i] = buffer;
}
// now use the array names[] however you want.
}
template<typename T>
A common template to unite T Abs(T x) {
them all . . . if (x < 0)
return -x;
else return x;
}
CS101 Autumn 2019 @ CSE IIT Bombay 48
Template Class
• Like function templates, create class with templates.
template <class T> main () {
class Queue { Queue<V3> q;
int front, nWaiting; Queue<int> r;
T elements[100];
r.insert(10);
public:
bool insert(T value)
v V3(1,1,1);
{...} q.insert(v);
bool remove(T &val)
{...} }
};
vector<int> v(10);
// somehow initialize v
sort(v.begin(), v.end());
double a[100];
// somehow initialize a
• Examples:
map<string, double> population;
Indices will have type string (country names), and elements
will have type double (population)
??
population[“India”] = 1.21;
// in billions. Map entry created
population[“China”] = 1.35;
population[“USA”] = 0.31;
population[“India”] = 1.22;
//update allowed
if(population.count(country)>0) {
// true if element with index = country
// was stored earlier
// count is a known member function
cout << population[country] << endl;
}
else cout << “Not known.\n”;
map<string,double>::iterator mi;
mi = population.begin();
// population.begin() : constant iterator
// points to the first element of population
// mi points to (India,1.21)
for(map<string,double>::iterator
mi = population.begin();
mi != population.end(); mi++)
for(map<string,double>::iterator
mi = population.begin();
mi != population.end();
mi++)
{
cout << (*mi).first << “: “ << (*mi).second << endl;
// or cout << mi->first << “: “ << mi->second << endl;
}
// will print out countries and population in
alphabetical order
map<string,double>::iterator
mi = population.find("India");
population.erase(mi);
cin >> N;
for(int i = 0; i < N; i++) {
int roll, int n;
cin >> roll >> n;
set<string> subjects;
• The library classes are very useful. Get some practice with them