[英]Sort Stack using vector
I'm new to stacks and vectors.我是堆栈和向量的新手。 I'm trying to sort a stack using vector but I am facing an error.我正在尝试使用向量对堆栈进行排序,但我遇到了错误。 Please suggest how to solve this error请建议如何解决此错误
MY CODE我的代码
/* The below method sorts the stack s
you are required to complete the below method */
void SortedStack :: sort()
{
vector<int> st;
while(!s.empty()){
st.push_back(s.top());
s.pop();
}
sort(st.begin(),st.end());
for (auto itr = st.begin(); itr != st.end(); ++itr) {
s.push(*itr);
}
}
Error generated产生的错误
Compilation Error:
Compilation Error
prog.cpp: In member function ‘void SortedStack::sort()’:
prog.cpp:61:27: error: no matching function for call to ‘SortedStack::sort(std::vector<int>::iterator, std::vector<int>::iterator)’
sort(st.begin(),st.end());
^
prog.cpp:52:6: note: candidate: void SortedStack::sort()
void SortedStack :: sort()
^
prog.cpp:52:6: note: candidate expects 0 arguments, 2 provided
sort(st.begin(), st.end());
is an attempt to call your sort
function and compilation fails as the parameters are incorrect.尝试调用您的sort
function 并且编译失败,因为参数不正确。
std::sort(st.begin(), st.end());
is a trivial fix.是一个微不足道的修复。
The moral of the story is to always use std::
explicitly, and drop using namespace std;
这个故事的寓意是始终显式使用std::
,并删除using namespace std;
&c.. Your code also becomes more readable as it's immediately obvious that you're using a function from the standard library. &c.. 您的代码也变得更具可读性,因为很明显您正在使用标准库中的 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.