如何解决使用向量对堆栈进行排序
我是堆栈和向量的新手。我正在尝试使用向量对堆栈进行排序,但是我遇到了一个错误。请提出解决此错误的方法
我的密码
/* 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);
}
}
产生错误
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());
试图调用您的sort
函数,由于参数不正确,编译失败。
std::sort(st.begin(),st.end());
是一个小问题。
故事的寓意是始终显式使用std::
,然后删除using namespace std;
&c。您的代码也变得更具可读性,因为很明显,您可以使用标准库中的函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。