我突然发现stl list竟然没有吧两个元素换位的函数

miliggate 2013-03-31 07:45:42
如题。。
今天用list的时候突然发现的,
我想要把某一个元素放到最前面,但是找不到这样的功能啊..
...全文
201 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tofu_ 2013-04-02
  • 打赏
  • 举报
回复
引用 11 楼 SisMVG 的回复:
引用 9 楼 akirya 的回复:当然有啊 list::splice C/C++ code?123456789101112131415161718192021#include <iostream>#include <list>using namespace std; int main(){ list<int> x; for( int i=0;i<10;……
这个函数很多人都没用过,也比较容易用错。
rocktyt 2013-04-02
  • 打赏
  • 举报
回复
原来splice是这么用的,又学到了
e_play 2013-04-01
  • 打赏
  • 举报
回复
优先队列,实现最大堆即可
miliggate 2013-04-01
  • 打赏
  • 举报
回复
引用 9 楼 akirya 的回复:
当然有啊 list::splice C/C++ code?123456789101112131415161718192021#include <iostream>#include <list>using namespace std; int main(){ list<int> x; for( int i=0;i<10;i++) { x……
而且这个不是用来粘合容器的吗? 自己的也可以?
miliggate 2013-04-01
  • 打赏
  • 举报
回复
引用 9 楼 akirya 的回复:
当然有啊 list::splice C/C++ code?123456789101112131415161718192021#include <iostream>#include <list>using namespace std; int main(){ list<int> x; for( int i=0;i<10;i++) { x……
..我看到我的vc11里有这个函数,但是E文苦手不认识。。 话说我以前好像没有看到过这个.
  • 打赏
  • 举报
回复
当然有啊 list::splice
#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> x;
	for( int i=0;i<10;i++)
	{
		x.push_back(i);
	}
	list<int>::iterator iter = x.begin();
	advance( iter , 5 );
	x.splice( x.begin() , x , iter );
	
	for( iter = x.begin() ; iter != x.end() ; iter++)
	{
		cout<<*iter<<" ";
	}
    return 0;
}
rocktyt 2013-04-01
  • 打赏
  • 举报
回复
如果你觉得因为元素体积太大导致iter_swap交换的效率过低,可以考虑在容器里存放指针
ghost5216 2013-04-01
  • 打赏
  • 举报
回复

// iter_swap.cpp 
// compile with: /EHsc
//
// Functions:
//   iter_swap  - Swap two elements in a sequence represented by
//                two iterators.
//
//   begin      - Returns an iterator that points to the first element
//                in a sequence.
//
//   end        - Returns an iterator that points one past the end of
//                  a sequence.

// disable warning C4786: symbol greater than 255 characters
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


// return the next Fibonacci number in the
// Fibonacci series.
int Fibonacci(void)
{
    static int r;
    static int f1 = 0;
    static int f2 = 1;
    r = f1 + f2 ;
    f1 = f2 ;
    f2 = r ;
    return f1 ;
}

int main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of integers
    typedef vector<int > IntVector ;

    //Define an iterator for template class vector of integer
    typedef IntVector::iterator IntVectorIt ;

    IntVector Numbers(VECTOR_SIZE) ;   //vector containing numbers

    IntVectorIt start, end, it ;

    start = Numbers.begin() ;   // location of first
                                // element of Numbers

    end = Numbers.end() ;       // one past the location
                                // last element of Numbers

    // fill the range [first, last +1) with a series of
    // Fibonacci numbers using the Fibonacci function
    generate(start, end, Fibonacci) ;

    cout << "Before calling iter_swap" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // swap the first and last elements of the
    // sequence using iter_swap
    iter_swap(start, end - 1) ;

    cout << "After calling iter_swap" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

}

Before calling iter_swap
Numbers { 1 1 2 3 5 8 13 21  }

After calling iter_swap
Numbers { 21 1 2 3 5 8 13 1  }
starytx 2013-04-01
  • 打赏
  • 举报
回复
嫌模板不好用那就自己搞个合适的链表了
翅膀又硬了 2013-04-01
  • 打赏
  • 举报
回复
你那需求可能没有普遍性
赵4老师 2013-04-01
  • 打赏
  • 举报
回复
std::iter_swap不是交换指向的意思?
miliggate 2013-04-01
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
不是有 std::swap 和 std::iter_swap 吗。list insert 也可以啊。
那个不是交换元素用的嘛,会copy一次元素, 我说的交换是只是把list的指向改变掉
mujiok2003 2013-03-31
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
不是有 std::swap 和 std::iter_swap 吗。list insert 也可以啊。
++++++
ri_aje 2013-03-31
  • 打赏
  • 举报
回复
不是有 std::swap 和 std::iter_swap 吗。list insert 也可以啊。

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧