![](/img/trans.png)
[英]How to print out the contents of a std::vector with std::copy_n?
[英]How can I print out the contents of std::stack and return its size?
在 C++ 中,如何打印堆栈的内容并返回其大小?
std::stack<int> values;
values.push(1);
values.push(2);
values.push(3);
// How do I print the stack?
您可以制作堆栈的副本并逐个弹出项目以转储它们:
#include <iostream>
#include <stack>
#include <string>
int main(int argc, const char *argv[])
{
std::stack<int> stack;
stack.push(1);
stack.push(3);
stack.push(7);
stack.push(19);
for (std::stack<int> dump = stack; !dump.empty(); dump.pop())
std::cout << dump.top() << '\n';
std::cout << "(" << stack.size() << " elements)\n";
return 0;
}
输出
19
7
3
1
(4 elements)
std::stack
和std::queue
都是通用容器的包装器。 该容器可作为protected
成员c
访问。 使用c
可以有效地访问元素; 否则,您可以只复制堆栈或队列并破坏性地访问副本的元素。
使用c
示例:
#include <iostream> // std::wcout, std::endl
#include <stack> // std::stack
#include <stddef.h> // ptrdiff_t
using namespace std;
typedef ptrdiff_t Size;
typedef Size Index;
template< class Elem >
Size nElements( stack< Elem > const& c )
{
return c.size();
}
void display( stack<int> const& numbers )
{
struct Hack
: public stack<int>
{
static int item( Index const i, stack<int> const& numbers )
{
return (numbers.*&Hack::c)[i];
}
};
wcout << numbers.size() << " numbers." << endl;
for( Index i = 0; i < nElements( numbers ); ++i )
{
wcout << " " << Hack::item( i, numbers ) << endl;
}
}
int main()
{
stack<int> numbers;
for( int i = 1; i <= 5; ++i ) { numbers.push( 100*i ); }
display( numbers );
}
打印std::stack
的元素而不弹出它们的唯一方法是编写一个扩展std::stack
的适配器(这是一个示例)。 否则,您应该用std::deque
替换您的堆栈。
在不使用“特殊技术”的情况下实现这一目标的一种简单方法是通过
递归
由于没有限制std::stack<>
pop ()
成员函数不能使用等,我们可以利用下面的递归算法。
算法:
尽管进行了弹出操作,但堆栈不会丢失其元素,因为我们在打印堆栈的其余部分后以相同的顺序再次推送它们。
下面是上述算法的代码:
void printStack (std::stack <int> &values) {
if (values.empty ()) {
return;
}
int topElement = values.top ();
values.pop ();
std::cout << topElement << std::endl;
printStack (values);
values.push (topElement);
}
上面代码的输出:
3
2
1
这将打印从上到下时尚的元素。
如果您希望元素从下到上打印,只需切换递归调用和std::cout
语句。
void printStack (std::stack <int> &values) {
if (values.empty ()) {
return;
}
int topElement = values.top ();
values.pop ();
printStack (values);
std::cout << topElement << std::endl;
values.push (topElement);
}
输出:
1
2
3
而std::stack<>
成员函数size ()
可以用来获取栈的大小。
std::cout << values.size () << std::endl;
嗯,一个将近 10 年的问题。 无论如何,这里有一个额外的答案。
第一:堆栈的大小由 std::stack.size() 给出。
然后,在现代 C++ 中,越来越多地使用带有算法的 STL。 因此,以下解决方案利用了这一点。 前提是堆栈使用连续内存。 这在目前是有保证的。
输出是通过单衬完成的。
请参阅以下示例:
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>
using Number = int;
using UnderlyingContainer = std::vector<Number>;
using Stack = std::stack< Number, UnderlyingContainer>;
std::istringstream testData("5 8 1 4 9 3");
int main()
{
// Put the test data onto the stack
Stack stack{ UnderlyingContainer {std::istream_iterator<Number>(testData),std::istream_iterator<Number>()} };
// Print the test data
if (not stack.empty())
std::copy(&stack.top() + 1 - stack.size(), &stack.top() + 1, std::ostream_iterator<Number>(std::cout, "\n"));
return 0;
}
这是完全有效和可靠的代码。 这里稍微解释一下。
我们想要输出数据,所以我们将它复制到一个 ostream_iterator。 ostream_iterator 引用一个流(是的,你也可以放一个开放的 ofstream)和定界符。 也许您想使用“ ”。
副本的来源是 2 个迭代器。 而且,是的,指针是迭代器。 并且,我们将保证的连续内存用于 std::stack。 因此,我们只需计算 2 个指针并将它们交给 std::copy。
如果你想使用显式迭代器。 开始了 。 .
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <sstream>
using Number = int;
using UnderlyingContainer = std::vector<Number>;
using Stack = std::stack< Number, UnderlyingContainer>;
using StackIterator = const Number *;
std::istringstream testData("5 8 1 4 9 3");
int main()
{
// Put the test data onto the stack
Stack stack{ UnderlyingContainer {std::istream_iterator<Number>(testData),std::istream_iterator<Number>()} };
// Print the test data
// Get iterators
StackIterator end = &stack.top() + 1;
StackIterator begin = end - stack.size();
if (not stack.empty())
std::copy(begin, end, std::ostream_iterator<Number>(std::cout, "\n"));
return 0;
}
因此,您可以为堆栈创建迭代器。 但是,警告:
std::stack 有意将其元素隐藏在引擎盖下。 因此,如果您对数据进行写访问,我会将其视为设计错误。 通过 const 指针/迭代器的读取访问对我来说是可以的。 但也许你应该更好地使用 std::vector 。 . .
http://www.cplusplus.com/reference/stl/stack/易于使用的大小:
cout << mystack.size();
其余的我在文档中没有看到任何内容,但是您应该在推送堆栈时打印堆栈的内容,或者有一个包含它的列表来保留元素的记录,只是为了打印它,不要完成测试后忘记删除它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.