gpt4 book ai didi

c++ - 将项目从 List move 到 Stack 或 Queue

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:14 25 4
gpt4 key购买 nike

我正在将 B1、P4、B4 B2、P6 等字符串添加到列表中。我试图将列表中的项目 move 到堆栈或队列中,直到列表为空。因此,如果项目字母是 P,我将其存储在队列中(后面或前面无关紧要)。如果字母是 B 那么我将它存储在堆栈中。我尝试像这样实现它,但出现此错误:

Boxes\main.cpp||In function 'int main()':|
Boxes\main.cpp|54|error: no matching function for call to 'std::queue<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::deque<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_queue.h|218|note: candidates are: void std::queue<_Tp, _Sequence>::push(const typename _Sequence::value_type&) [with _Tp = std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Sequence = std::deque<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::list<std::b|
Boxes\main.cpp|56|error: no matching function for call to 'std::stack<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::deque<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_stack.h|182|note: candidates are: void std::stack<_Tp, _Sequence>::push(const typename _Sequence::value_type&) [with _Tp = std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Sequence = std::deque<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::list<std::b|

如有任何建议,我们将不胜感激。

这是我的:



#include <iostream>

using namespace std;

#include <list>
#include <queue>
#include <stack>
template <typename T> // prototype for client function
void print(list<T> &);

int main()
{
list<string> lst;
queue< list<string> > package;
stack< list<string> > box;
string choice, str;
string::size_type pos, start_position=0;
bool choice_flag = true;

do{
cin >> choice;
if(choice == "QUIT"){
choice_flag = false;
break;
}
else{
lst.push_back(choice);
}
}while(choice_flag);

cout << "the size of the list is " << lst.size() << endl;

if (lst.empty() == true)
cout << "the list is empty" << endl << endl;
else
cout << "the list is not empty" << endl << endl;

print(lst);

// attempt to move from list lst and push onto queue or stack
while(!lst.empty()){
str = lst.front();
if(str.find('P' ,0))
package.push(str);
else
box.push(str);
}

return 0;
}

最佳答案

如果你想改变一个队列和一个字符串栈:

queue< list<string> > package;
stack< list<string> > box;

到:

queue< string > package;
stack< string > box;

关于c++ - 将项目从 List move 到 Stack 或 Queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com