Anda di halaman 1dari 15

Kompetensi : Mahasiswa mampu

menggunakan metode begin(), end()


dan iterator dalam suatu kontainer
seperti Vector, List dan lainnya
#8.
21 Mei 2015

Mendefinisikan vector beserta iteratornya, mengisi vector, min,


max, sort, reverse, mencetak Iterator
include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> coll;
// vector container for ints
std::vector<int>::iterator pos; // iterator
// insert elements 1 to 6 into the collection, unsorted
coll.push_back(2);
coll.push_back(5);
coll.push_back(4);
coll.push_back(1);
coll.push_back(6);
coll.push_back(3);
Bersambung

Lanjutan 1
// output minimum and maximum element
pos = std::min_element(coll.begin(), coll.end());
std::cout << "min: " << *pos << std::endl;
pos = std::max_element(coll.begin(), coll.end());
std::cout << "max: " << *pos << std::endl;
// sort all elements elements in ascending order
std::sort(coll.begin(), coll.end());
// reverse order of elements
std::reverse(coll.begin(), coll.end());
// output all elements
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}

Test 1-#1

#include <iostream.h>
#include <list.h>
int main()
{
list<int> coll; // list container for integer elements
// append elements from 1 to 50
for (int c=1; c<=50; ++c) {
coll.push_back(c);
}
Tuliskan Output
system("PAUSE");
Program Tersebut ?
return 0;
}

#2

#include <iostream.h>
#include <list.h>
int main()
{
list< int> coll;
// append elements from 1 to 20
for (int c=1; c<=20; ++c) {
coll.push_front(c);
}
Tuliskan Output
system("PAUSE");
Program Tersebut ?
return 0;
}

#3

#include <iostream.h>
#include <list.h>
int main()
{
list<int> coll; // list container for integer elements
// append elements from 1 to 20
for (int c=1; c<=20; ++c) {
coll.push_front(c);
}
while (! coll.empty()) {
cout << coll.back() << ' ';
coll.pop_back();
}
Tuliskan Output
cout << endl;
Program Tersebut ?
system("PAUSE");
return 0;
}

#4
Ubahlah program nomor 3 sehingga dapat
digunakan untuk menampung object
mahasiswa.

#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
int main()
{
int coll[] = { 5, 6, 2, 4, 1, 3 }; // square all elements
transform (coll, coll+6,
// first source
coll,
// second source
coll,
// destination
multiplies<int>()); // operation // sort beginning with the second element
sort (coll+1, coll+6); // print all elements
copy (coll, coll+6, ostream_iterator<int>(cout," "));
cout << endl;
system("PAUSE");
return 0;
}

Vector
#include <iostream.h>
#include <stdlib.h>
#include <vector>
int main()
{
std::vector<int> coll; // container vector bertipe integer
// insert elements with the values 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_back(i);
}
// output all elements followed by a space
for (int i=0; i<coll.size(); ++i) {
std::cout << coll[i] << ' ';
}
// finally output a newline
std::cout << std::endl;
system("PAUSE");
return 0;
}

#include <iostream.h>
#include <stdlib.h>
#include <vector>
int increment(int i) { return ++i; }
int sum(int a, int b) { return a+b; }
int kuadrat(int a) {return a*a};
int main()
{
vector<int> v1;
for(int i=1; i<6; i++)
{ v1.push_back (i); }
// v1: 1 2 3 4 5
vector<int> v2(v1.size());
transform(v1.begin(), v1.end(), v1.begin(), kuadrat); // v1: 1 4 9 16 25
transform(v1.begin(), v1.end(), v2.begin(), increment); // v2: 2 5 10 17 26
// add the elements of v1 and v2 together, store the result in v1
transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), sum);
// v1: 3 9 19 33 51
// output all elements followed by a space
for (int i=0; i<v1.size(); ++i)
{ cout << v1[i] << ' ; } // printf(%d , v1[i])
cout << endl;
system("PAUSE");
return 0;
}

Sintaks Transform
output_iterator transform( input_iterator
start1, input_iterator end1, input_iterator2
start2, output_iterator result, BinaryFunction f
);

#include <iostream.h>
#include <stdlib.h>
#include <vector>
int increment(int i) { return ++i; }
int sum(int a, int b) { return a+b; }
int main()
{
vector<int> v1;
for(int i=1; i<6; i++)
{ v1.push_back (i*i); }
// v1: 1 4 9 16 25
vector<int> v2(v1.size());
transform(v1.begin(), v1.end(), v2.begin(), increment); // v2: 2 5 10 17 26
// add the elements of v1 and v2 together, store the result in v1
transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), sum);
// v1: 3 9 19 33 51
// output all elements followed by a space
for (int i=0; i<v1.size(); ++i)
{ cout << v1[i] << ' ; } // printf(%d , v1[i])
cout << endl;
system("PAUSE");
return 0;
}

Initial State
V1 = 1 2 3 4 5
V2 = {} (= kosong)
Final State
V1 = 3 9 19 33 51
V2 = (ada isinya)

Soal
Cari tentang contoh kasus kosong dalam
pemrograman + source code yang
menunjukkan kasus kosong
Cari info tentang mesin karakter dan mesin
kata
(kelompok)

Kuis
2. Diketahui:
Kondisi awal a=1 2 3 4 5, b=a
Nim genap: Kondisi akhir a=0 4 0 2 0, b=5 4 3 2 1
Nim ganjil: Kondisi akhir a=5 0 3 0 1, b=5 4 3 2 1