堆栈stack容器
与queue和priority_queue一起被归类为适配器
意味着它们将通过调整某一个基本序列容器以存储自己的数据
显示实现stack<string>的3种方式
默认方式 使用deque
然后采用vector的方式
最后一个采用list方式
//: C07:Stack1.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Demonstrates the STL stack.
#include <fstream>
#include <iostream>
#include <list>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// Rearrange comments below to use different versions.
typedef stack<string> Stack1; // Default: deque<string>
// typedef stack<string, vector<string> > Stack2;
// typedef stack<string, list<string> > Stack3;
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();
}
getchar();
} ///:~
输出
} ///:~
getchar();
}
textlines.pop();
cout << textlines.top();
while(!textlines.empty()) {
// Print lines from the stack and pop them:
textlines.push(line + "\n");
while(getline(in, line))
string line;
// Read file and store lines in the stack:
Stack1 textlines; // Try the different versions
ifstream in("Stack1.cpp");
int main() {
// typedef stack<string, list<string> > Stack3;
// typedef stack<string, vector<string> > Stack2;
typedef stack<string> Stack1; // Default: deque<string>
// Rearrange comments below to use different versions.
using namespace std;
#include <vector>
#include <string>
#include <stack>
#include <list>
#include <iostream>
#include <fstream>
// Demonstrates the STL stack.
// distributed with the code package available at www.MindView.net.
// See source code use permissions stated in the file 'License.txt',
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
//: C07:Stack1.cpp
如果使用过其他stack类的话
top()和pop()操作似乎不直观
当调用pop()时
返回一个void值而不是所预期的栈顶元素程序存储文件中的每一行
与该行中的前导空白字符的个数一起存储
//: C07:Stack2.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Converting a list to a stack.
#include <iostream>
#include <fstream>
#include <stack>
#include <list>
#include <string>
#include <cstddef>
using namespace std;
// Expects a stack:
template<class Stk>
void stackOut(Stk& s, ostream& os = cout) {
while(!s.empty()) {
os << s.top() << "\n";
s.pop();
}
}
class Line {
string line; // Without leading spaces
size_t lspaces; // Number of leading spaces
public:
Line(string s) : line(s) {
lspaces = line.find_first_not_of(' ');
if(lspaces == string::npos)
lspaces = 0;
line = line.substr(lspaces);
}
friend ostream& operator<<(ostream& os, const Line& l) {
for(size_t i = 0; i < l.lspaces; i++)
os << ' ';
return os << l.line;
}
// Other functions here...
};
int main() {
ifstream in("Stack2.cpp");
list<Line> lines;
// Read file and store lines in the list:
string s;
while(getline(in, s))
lines.push_front(s);
// Turn the list into a stack for printing:
stack<Line, list<Line> > stk(lines);
stackOut(stk);
getchar();
} ///:~
输出
//: C07:Stack2.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Converting a list to a stack.
#include <iostream>
#include <fstream>
#include <stack>
#include <list>
#include <string>
#include <cstddef>
using namespace std;
// Expects a stack:
template<class Stk>
void stackOut(Stk& s, ostream& os = cout) {
while(!s.empty()) {
os << s.top() << "\n";
s.pop();
}
}
class Line {
string line; // Without leading spaces
size_t lspaces; // Number of leading spaces
public:
Line(string s) : line(s) {
lspaces = line.find_first_not_of(' ');
if(lspaces == string::npos)
lspaces = 0;
line = line.substr(lspaces);
}
friend ostream& operator<<(ostream& os, const Line& l) {
for(size_t i = 0; i < l.lspaces; i++)
os << ' ';
return os << l.line;
}
// Other functions here...
};
int main() {
ifstream in("Stack2.cpp");
list<Line> lines;
// Read file and store lines in the list:
string s;
while(getline(in, s))
lines.push_front(s);
// Turn the list into a stack for printing:
stack<Line, list<Line> > stk(lines);
stackOut(stk);
getchar();
} ///:~
需要stack接口的函数仅仅发送每个top()对象到一个ostream
然后通过调用pop()将其删除
Line类判断前导空白字符的个数
然后存储没有这些前导空白字符的行的内容
不能从头至尾对一个Stack进行迭代
当创建一个stack时
只能希望对其进行stack操作
//: C07:Stack3.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Using a vector as a stack; modified Stack1.cpp.
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream in("Stack3.cpp");
vector<string> textlines;
string line;
while(getline(in, line))
textlines.push_back(line + "\n");
while(!textlines.empty()) {
cout << textlines.back();
textlines.pop_back();
}
getchar();
} ///:~
这个程序产生像Stack1.cpp一样输出
但现在还可以进行与vector一样的操作
list也可以将元素压入前端
输出 文件放工程目录下
} ///:~
getchar();
}
textlines.pop_back();
cout << textlines.back();
while(!textlines.empty()) {
textlines.push_back(line + "\n");
while(getline(in, line))
string line;
vector<string> textlines;
ifstream in("Stack3.cpp");
int main() {
using namespace std;
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
// Using a vector as a stack; modified Stack1.cpp.
// distributed with the code package available at www.MindView.net.
// See source code use permissions stated in the file 'License.txt',
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
//: C07:Stack3.cpp