/*
*list事例程序
*/
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> coll;
for (char c = 'a'; c <= 'z'; ++c) //++c比c++效率高,因为后者需要一个额外的临时对象。
{
coll.push_back(c);
}
while(!coll.empty())
{
cout << coll.front() << ' '; //The front member function returns a reference to the first element
//of the controlled sequence, which must be nonempty. back函数类似
coll.pop_front();
}
getchar();
}
/*
*multimap事例程序
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
typedef multimap<int, string> IntStringMMap;
IntStringMMap coll;
coll.insert(make_pair(5, "tagged"));
coll.insert(make_pair(2, "a"));
coll.insert(make_pair(1, "this"));
coll.insert(make_pair(4, "of"));
coll.insert(make_pair(6, "strings"));
coll.insert(make_pair(1, "is"));
coll.insert(make_pair(3, "multimap"));
IntStringMMap::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << pos->second << ' ';
}
cout << endl;
getchar();
}
/*
*算法事例程序
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll;
vector<int>::iterator pos;
coll.push_back(2);
coll.push_back(5);
coll.push_back(4);
coll.push_back(1);
coll.push_back(6);
coll.push_back(3);
pos = min_element(coll.begin(), coll.end());
cout << "min: " << *pos << endl;
pos = max_element(coll.begin(), coll.end());
cout << "max: " << *pos << endl;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << ' ';
}
cout << endl;
sort(coll.begin(), coll.end());
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << ' ';
}
cout << endl;
pos = find(coll.begin(), coll.end(), 3);
reverse(pos, coll.end());
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << *pos << ' ';
}
cout << endl;
getchar();
}
/*
*非随机存取迭代器,空间书写时如何判断两迭代器的前后顺序
*如:(*posM) = m,(*posN) = n,[pos?, pos?)
*/
posM = find(coll.begin(), coll.end(), m);
posN = find(coll.begin(), posM, n);
if (posM != posN)
{
//[posN, posM) is valid.
}
else
{
posN = find(posM, coll.end(), N);
if (posM != posN)
{
//[posN, posM) is valid.
}
else
{
//posM == posN;
}
}
/*
*安插型迭代器事例程序
*/
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll1;
for (int i= 1; i <= 9; ++i)
{
coll1.push_back(i);
}
vector<int> coll2;
copy(coll1.begin(), coll1.end(), back_inserter(coll2));
deque<int> coll3;
copy(coll1.begin(), coll1.end(), front_inserter(coll3));
set<int> coll4;
copy(coll1.begin(), coll1.end(), inserter(coll4, coll4.begin()));
getchar();
}
/*
*流迭代器事例程序
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> coll;
copy(istream_iterator<string> (cin), istream_iterator<string> (), back_inserter(coll)); //CTRL+Z退出输入
sort(coll.begin(), coll.end());
unique_copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
}
/*
*逆向迭代器事例程序
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll;
for (int i = 1; i <= 9; ++i)
{
coll.push_back(i);
}
copy(coll.rbegin(), coll.rend(), ostream_iterator<int> (cout, " "));
cout << endl;
getchar();
}
//print.hpp
#include <iostream>
/* PRINT_ELEMENTS()
*- print optional C-string optcstr followed by
*- all elements of the collection coll
*- separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr = "")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
/*
*PRINT_ELEMENTS()事例程序
*/
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include "print.hpp"
int square(int value)
{
return value*value;
}
int main()
{
std::set<int> coll1;
std::vector<int> coll2;
for (int i = 1; i <= 9; ++i)
{
coll1.insert(i);
}
PRINT_ELEMENTS(coll1, "initialized: ");
std::transform(coll1.begin(), coll1.end(), std::back_inserter(coll2), square);
PRINT_ELEMENTS(coll2, "squared: ");
getchar();
}
/*
*仿函数事例程序
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//simple function object that prints the passed argument
class PrintInt
{
public:
void operator() (int elem) const
{
cout << elem << ' ';
}
};
int main()
{
vector<int> coll;
for (int i = 1; i <= 9; ++i)
{
coll.push_back(i);
}
for_each(coll.begin(), coll.end(), PrintInt());
cout << endl;
getchar();
}