标准模板类(STL)(六),举例
三、举例说明
在前面介绍过程中,已有相应的例子了。现在在学习几个常用的例子。
1、普通指针处理和运用STL的区别
Node* currNode = head;
while (currNode != NULL) {
cout << currNode->data << endl;
currNode = currNode->next;
}
++++++++++++使用STL迭代器+++++++++++++++++
ListIterator listIter;
listIter.Reset(list);
while (!listIter) {
cout << *listIter << " ";
listIter++;
}
2、struct pair
template <class U, class V>
struct pair {
U first;
V second;
pair(const U& first = U(), const V& second = V()) :first(first), second(second) {}
};
template <class U, class Y>
pair<U, V> make_pair(const U& first, const V& second);
The make_pair function creates a pair structure that contains two data elements of any type。
我们注意到,它是一个结构体,而不是类,默认下,其成员是公有的。一个例子如下:
#include "iostream"
#include "utility"
using namespace std;
typedef struct pair<int,float> PAIR_IF;
int main(void)
{
PAIR_IF pair1=make_pair(18,3.14f);
cout<<pair1.first<< " "<<pair1.second<<endl;
pair1.first=10;
pair1.second=1.0f;
cout << pair1.first << " " << pair1.second << endl;
return 1;
}
All of the associative containers (map,hash_map, and multimap)
require a pair be used to insert new data.
map<string, int> portfolio;
portfolio.insert(make_pair(string("LU"), 400));//portfolio["LU"] = 400;
portfolio.insert(make_pair(string("AAPL"), 80));//portfolio["AAPL"] = 80;
portfolio.insert(make_pair(string("GOOG"), 6500));//portfolio["GOOG"] = 6500;
3、map
template <class Key, class Value>
class map {
public:
map();
map(const map<Key, Value>& originalMap);
// typedefs for iterator and const_iterator
pair<iterator, bool> insert(const pair<Key, Value>& newEntry);
iterator find(const Key& key);
const_iterator find(const Key& key) const;
Value& operator[](const Key& key);
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
};
说明一下insert函数:
pair<iterator, bool> insert(const value_type& x);
The member function determines whether an element y exists in the sequence whose
key matches that of x. (The keys match if !key_comp()(x. first, y.first) && !key_comp()(y.first, x.first).) If not, it creates such an element y and initializes it with x. The function
then determines the iterator it that designates y. If an insertion occurred, the function
returns pair(it, true). Otherwise, it returns pair(it, false).
举个例子:
#include "iostream"
#include "vector"
#include "map"
using namespace std;
typedef map<string,vector<string> > chordMap;
int main()
{
chordMap jazzChords;
vector<string> cmajor;
cmajor.push_back("C");
cmajor.push_back("E");
cmajor.push_back("G");
pair<chordMap::iterator, bool> result=\
jazzChords.insert(make_pair(string("C Major"), cmajor));
if (result.second)
cout<<"result.first->first was successfully inserted."<< endl;
else
cout<<"result.first->first already present." << endl;
return 1;
}
+++++++++++++++++
#include "iostream"
#include "list"
#include "string"
#include "map"
using namespace std;
int main()
{
map<string,int,less<string> >name_age;
name_age["Poh1,Laura"]=7;
name_age["Dolsberry,Betty"]=9;
name_age["Pohl,Tanya"]=14;
cout<<"laura is "<<name_age["Poh1,Laura"]
<<" Years old."<<endl; // output is 7
}
4、istream_iterator
STL provides adaptors, types that transform the interface of other types.
This is very much how electrical adaptors work. One very useful adaptor is
istream_iterator. This is a template type; you parameterize it by the type of
object you want to read from the stream. In this case we want integers, so we
would use an istream_iterator<int>. Istream iterators are initialized by giving
them a stream, and thereafter, dereferencing the iterator reads an element
from the stream,and incrementing the iterator has no effect. An istream
iterator that is created with the default constructor has the past-the-end value.
In order to read the elements into the vector from standard input, we will use
the STL copy algorithm; this takes three iterators. The first two specify the
source range, and the third specifies the destination.
typedef istream_iterator<int> istream_iterator_int;
copy(istream_iterator_int(cin),istream_iterator_int(),v.begin());
The first iterator will be incremented and read from until it is equal to the
second iterator. The second iterator is just created with the default
constructor; this gives it the past-the-end value. The first iterator will also
have this value when the end of the stream is reached. Therefore the
rangespecified by these two iterators is from the current position in the input
stream to the end of the stream.
此外,back_insert_iterator也是一个常用的迭代器,上述程序中,有一个问题,就是v如果没能足够的空间就会越界。而用back_insert_iterator解决这个问题。
istream_iterator_int start (cin);
istream_iterator_int end;
back_insert_iterator<vector<int> > dest (v);
copy (start, end, dest);
Similarly, to print out the values after sorting, we use copy:
copy (v.begin(), v.end(), ostream_iterator<int> (cout, "\n"));