在C++中使用队列交换方法时的编译错误

Rah*_*obi 1 c++ linux queue g++

error: ‘class std::queue<int>’ has no member named ‘swap’编译下面的代码时

#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> foo,bar;
  foo.push (10); foo.push(20); foo.push(30);
  bar.push (111); bar.push(222);

  foo.swap(bar);

  std::cout << "size of foo: " << foo.size() << '\n';
  std::cout << "size of bar: " << bar.size() << '\n';

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3用于编译此代码,任何人都可以对此错误有所了解吗?

mar*_*inj 5

使用:

std::swap(foo, bar);
Run Code Online (Sandbox Code Playgroud)

看来,从c ++ 11开始,你就有了std :: queue :: swap

http://www.cplusplus.com/reference/queue/queue/swap-free/

g ++ 4.6似乎不接受-std = c ++ 11,因此您必须升级编译器才能使用此方法.

[编辑]

g ++ 4.6接受-std = c ++ 0x来启用c ++ 11