vector<int>::iterator p = a_vector.begin();
cout << *p << "\n";
container C;
. . .
container::iterator p=C.begin(), q=C.end();
while (p != q)
{
cout << *p << "\n";
++p;
}
| Iterators | Operations |
|---|---|
| Input Iterator | ==, !=, ++, and * (only for returning a value) |
| Output Iterator | ==, !=, ++, and * (only for assignment) |
| Forward Iterator | ==, !=, ++, and * |
| Bidirectional Iterator | ==, !=, ++, --, and * |
| Random Access Iterator | ==, !=, ++, --, [], *, as well as iter + n and iter - iter |
template<typename Iterator, typename Action>
void for_each(Iterator current, Iterator stop,
Action action)
{
while (current != stop)
{
action(*current);
++current;
}
}
void print_element(int value)
{
cout << value << "\n";
}
list<int> a_list;
. . .
for_each(a_list.begin(), a_list.end(), print_element);
bool is_even(int val)
{
return 0 == val % 2;
}
bool is_leap_year(int year)
{
// Every 400 years is a leap year
if (0 == year % 400) return true;
// Otherwise centuries are not
if (0 == year % 100) return false;
// Every fourth year is
if (0 == year % 4) return true;
return false; // Otherwise not
}
list<int>::iterator first_leap =find_if(a_list.begin(), a_list.end(), is_leap_year); if (first_leap != a_list.end()) . . . // Found it
vector<int> a(10);
generate(a.begin(), a.end(), rand);
int rand_one_hundred()
{
return 1 + rand() % 100;
}
vector<int> b(50);
generate(b.begin(), b.end(), rand_one_hundred);
class RandomInt
{
public:
RandomInt(int ia, int ib);
int operator()();
private:
int a, b;
};
RandomInt::RandomInt(int ia, int ib)
{
a = ia;
b = ib;
}
inline int RandomInt::operator()()
{
return a + rand() % (b - a + 1);
}
RandomInt r10(1,10);
cout << r10() << "\n";
vector<int> b(20);
generate(b.begin(), b.end(), r10);
generate(b.begin(), b.end(), RandomInt(1, 10));
class DivisibleBy {
public:
DivisibleBy(int in);
bool operator()(int x);
private:
int n;
};
DivisibleBy::DivisibleBy(int in) : n(in) {}
inline bool DivisibleBy::operator()(int x)
{ return 0 == x % n; }
list<int> a_list; . . . list<int>::iterator current = a_list.begin(); DivisibleBy pred(*current); // Create the predicate ++current; // Advance to next element list<int>::iterator ele = find_if(current, a_list.end(), pred); if (ele != a_list.end()) // Found it
class SequenceGenerator {
public:
// Can set starting value
SequenceGenerator(int sv);
int operator()();
private:
int current;
};
SequenceGenerator::SequenceGenerator(int sv)
{ current = sv; }
inline int SequenceGenerator::operator()()
{
int r = current;
current++;
return r;
}
vector<int> a(20); // Declare a new vector
generator(a.begin(), a.end(), SequenceGenerator(1)); // Initialize it
vectora(10);
fill(a.begin(), a.end(), 1);
fill_n(a.begin(), 5, 2);
vector<int> b(20);
copy(a.begin(), a.end(), b.begin());
// Will initialize first 10 positions of b
vectorc(20);
generate(c.begin(), c.end(), RandomInt(50, 75));
generate_n(c.begin(), 10, RandomInt(10, 20)); // Overwrite first 10 positions
vector<int> a(10);
// Create list of random numbers
generate(a.begin(), a.end(), rand);
sort(a.begin(), a.end()); // Then sort them
vector<int> a(10);
generate(a.begin(), a.end(), SequenceGenerator(1));
// Initially 1, 2, . . . 10
reverse(a.begin(), a.end());
// Now 10, 9, 8, . . . 2, 1
vector<int> a(10);
generate(a.begin(), a.end(), SequenceGenerator(1));
// Initially 1, 2, . . . 10
random_shuffle(a.begin(), a.end());
vector<int> a(10);
generate(a.begin(), a.end(), SequenceGenerator(1));
vector<int>::iterator rotate_point
= find(a.begin(), a.end(), 7);
rotate(a.begin(), rotate_point, a.end());
| 7 | 8 | 9 | 10 | 1 | 2 | 3 | 4 | 5 | 6 |
vector<int> a(10);
generate(a.begin(), a.end(), SequenceGenerator(1));
partition(a.begin(), a.end(), is_even);
| 10 | 8 | 6 | 4 | 2 | 3 | 1 | 7 | 5 | 9 |
int square(int n)
{
return n * n;
}
vector<int> a(10);
generate(a.begin(), a.end(), SequenceGenerator(1));
vector<int> b(10);
transform(a.begin(), a.end(), b.begin(), square);
int add(int a, int b)
{
return a + b;
}
vector<int> c(10);
transform(a.begin(), a.end(), b.begin(), c.begin(), add);
vector<int> a(10);
vector<int> b(20);
generate_n(a.begin(), 10, SequenceGenerator(1));
generate_n(b.begin(), 20, SequenceGenerator(1));
vector<int> c(30);
merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
vector<int> a(4);
// Initialize 1, 2, 3, 4
generate(a.begin(), a.end(), SequenceGenerator(1));
while (next_permutation(a.begin(), a.end()))
{
cout << "Output permutation ";
vector<int>::iterator p = a.begin();
while (p != a.end())
{
cout << *p << " ";
++p;
}
cout << "\n";
}
vector<int> d(10);
// Generate ten random numbers
generate(d.begin(), d.end(), rand);
// Find largest value
vector<int>::iterator mx =
max_element(d.begin(), d.end());
// Find smallest value
vector<int>::iterator mn =
min_element(d.begin(), d.end());
if (mx != d.end()) // Will fail for empty vector
cout << "Largest is " << *mx
<< " and smallest is " << *mn << "\n";
void Inventory::receive(Product newp)
{
cout << "Received Shipment of product "
<< newp << "\n";
list<int>::iterator we_need =
find(on_order.begin(), on_order.end(), newp.get_id());
if (we_need != on_order.end())
{
cout << "Ship " << newp
<< " to fill back order\n";
on_order.erase(we_need);
}
else
on_hand.push_front(newp);
}
list<int> years;
. . .
list<int>::iterator first_leap =
find_if( years.begin(), years.end(), is_leap_year);
vector<int> a(20);
generate(a.begin(), a.end(), RandomInt(1, 10));
sort(a.begin(), a.end());
if (binary_search(a.begin(), a.end(), 7))
{
vector<int>::iterator p
= lower_bound(a.begin(), a.end(), 7);
vector<int>::iterator q
= upper_bound(a.begin(), a.end(), 7);
while (p != q)
{
cout << "found " << *p << "\n";
++p;
}
}
a 1 2 3 2 4 2 5 6 b 0 0 0 0 0 0 0 0
a 1 2 3 2 4 2 5 6 b 1 3 4 5 6 0 0 0
a 1 2 3 2 4 2 5 6 b 1 3 5 0 0 0 0 0
vector<int>::iterator p;
p = remove_copy_if(a.begin(), a.end(),
b.begin(), DivisibleBy(2));
b.erase(p,b.end());
b 1 3 5
a 1 2 3 2 4 2 5 6
p = remove(a.begin(), a.end(), 2);
a 1 3 4 5 6 2 5 6
void sieve(deque<int>& numbers); deque<int> a(100); generate_n(a.begin(), 100, SequenceGenerator(2)); sieve(a);
void sieve(deque<int>& numbers)
{
if (numbers.empty())
return;
int n = numbers.front();
numbers.pop_front();
deque<int>::iterator p = remove_if(numbers.begin(),
numbers.end(), DivisibleBy(n));
numbers.erase(p, numbers.end());
sieve(numbers);
numbers.push_front(n);
}
list<int>::iterator p =
unique(a.begin(), a.end());
a.erase(p, a.end());
// Remove the remaining values
vector<int> a(10);
generate(a.begin(), a.end(), RandomInt(1, 5));
replace(a.begin(), a.end(), 3, 4);
replace_if(a.begin(), a.end(), is_even, 0);
int three_count = count(a.begin(), a.end(), 3);
int even_count = count_if(a.begin(), a.end(), is_even);
cout << "number of 3's " << three_count <<
" number even " << even_count << "\n";
list<double> data;
double sum = accumulate(data.begin(), data.end(), 0.0);
// Now sum contains the sum of the elements in the list
list<int> b;
copy(a.begin(), a.end(), back_inserter(b));
list<int> c;
generate_n(front_inserter(c), 20, RandomInt(2, 10));
copy(c.begin(), c.end(), ostream_iterator(cout) );
copy(c.begin(), c.end(), ostream_iterator(cout, "\n") );
list<int> d;
copy(istream_iterator<int>(cin),
istream_iterator<int>(), back_inserter(d));
int main()
{
vector<int> x;
copy(istream_iterator<int>(cin),
istream_iterator(), back_inserter(x));
vector<int>::iterator p =
remove_if(x.begin(), x.end(), is_even);
x.erase(p, x.end());
copy(x.begin(), x.end(),
ostream_iterator<int>(cout, "\n"));
return 0;
}
set<int, greater<int> > new_set;
vector<int> a(100);
vector<int> b(100);
// Initialize a and b with values
vector<int> c(100);
transform(a.begin(), a.end(),
b.begin(), c.begin(), plus<int>());
int prod = accumulate(a.begin(), a.end(),
1, times<int>());
list<int>::iterator first_negative =
find_if( a.begin(), a.end(),
bind2nd(less<int>(), 0) );
list<int>::iterator first_positive =
find_if( a.begin(), a.end(),
not1( bind2nd( less<int>(), 0 )) );