将项目从List移动​​到堆栈或队列

时间:2011-11-07 04:31:58

标签: c++ list stack queue move

我在列表中添加B1,P4,B4 B2,P6等字符串。我试图将列表中的项目移动到堆栈或队列,直到列表为空。因此,如果项目字母是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;
}

1 个答案:

答案 0 :(得分:1)

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

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

为:

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