copy(IsbIt(in2), IsbIt(), back_inserter(dc));
TokenIterator
dcIter(dc.begin(), dc.end(), delimiters),
end3;
vector
copy(dcIter, end3, back_inserter(wordlist3));
copy(wordlist3.begin(), wordlist3.end(), out);
*out++ = "-----------------------------------";
// Reproduce the Wordlist.cpp example:
ifstream in3("TokenIteratorTest.cpp");
TokenIterator
wordIter2((IsbIt(in3)), isbEnd, delimiters);
set
while(wordIter2 != end)
wordlist4.insert(*wordIter2++);
copy(wordlist4.begin(), wordlist4.end(), out);
} ///:~
When using an istreambuf_iterator, you create one to attach to the istream object and one with the default constructor as the past-the-end marker. Both are used to create the TokenIterator that will actually produce the tokens; the default constructor produces the faux TokenIterator past-the-end sentinel. (This is just a placeholder and, as mentioned previously, is actually ignored.) The TokenIterator produces strings that are inserted into a container which must, naturally, be a container of string—here a vector
The strangest thing in the previous program is the declaration of wordIter2. Note the extra parentheses in the first argument to the constructor. Without these, a conforming compiler will think that wordIter2 is a prototype for a function that has three arguments and returns a TokenIterator
stack
The stack, along with the queue and priority_queue, are classified as
The following example shows stack
//: C07:Stack1.cpp
// Demonstrates the STL stack
#include
#include
#include
#include
#include
#include
using namespace std;
// Rearrange comments below to use different versions.
typedef stack
// typedef stack
// typedef stack
int main() {
ifstream in("Stack1.cpp");
Stack1 textlines; // Try the different versions
// Read file and store lines in the stack:
string line;
while(getline(in, line))
textlines.push(line + "\n");
// Print lines from the stack and pop them:
while(!textlines.empty()) {
cout << textlines.top();
textlines.pop();
}
} ///:~
The top( ) and pop( ) operations will probably seem non-intuitive if you’ve used other stack classes. When you call pop( ), it returns void rather than the top element that you might have expected. If you want the top element, you get a reference to it with top( ). It turns out this is more efficient, since a traditional pop( ) would have to return a value rather than a reference and thus invoke the copy-constructor. More important, it is