STL中的list就是一双向链表,可高效地进行插入删除元素。 List 是 C++标准程式库 中的一个  ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合。list 的特色是在集合的任何位置增加或删除元素都很快,但是不支持随机存取。list 是 C++标准程式库 提供的众多容器(container)之一,除此之外还有 vector 、set、map、…等等。list 以模板方式实现(即泛型),可以处理任意型别的变量,包括使用者自定义的资料型态,例如:它可以是一个放置整数(int)型态的 list、也可以是放置字串(char 或 string)型态的 list、或者放置使用者自定类别(user-defined class)的 list。

list 被定义在 <list> 标头档中。一如其他STL元件,list属于std名称空间。

list 内部以 数据结构 的双向 连结串行 实做,内部元素并非放置于连续大块内存中,而是散落于内存各处,互相以link串接起来,每个元素都只知道其前一个元素以及下一个元素的位置。故要走访整个list,必须从第一个元素开始逐个往下寻访,不支持随机存取(Random Access)。 list 的强项是高效的插入以及删除,于list插入或删除时只需要改动元素的link字段,不需要搬动元素,代价相对便宜。

list 在经常需要于集合内部任意位置(即除了头尾以外的其他位置) 频繁增删元素的工作上表现优秀。若仅需要于集合尾端增删元素,那应该优先考虑 vector 容器,若仅于头尾二端增删元素,那应该优先考虑 deque 容器。

成员函数概观
Iterators:
list.begin() 回传指向第一个元素的 Iterator。
list.end() 回传指向最末元素的下一个位置的 Iterator。
list.rbegin() 回传指向最末个元素的反向 Iterator。
list.rend() 回传指向第一个元素的前一个位置的反向 Iterator。 Capacity/Size:
list.empty() 若list内部为空,则回传true值。
list.size() 回传list内实际的元素个数。
lsit.resize() 重新分派list的长度。 Element Access
list.front() 存取第一个元素。
list.back() 存取最末个元素。 Modify methods
list.push_front() 增加一个新的元素在 list 的前端。
list.pop_front() 删除 list 的第一个元素。
list.push_back() 增加一个新的元素在 list 的尾端。
list.pop_back() 删除 list 的最末个元素。

leetcode: Rotate Array

class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(nums.size() == || nums.size() == ) return; list<int> ls; ls.clear();
list<int>::iterator pt; for(int i=;i<nums.size();++i) ls.push_back(nums[i]); int i=;
k%=nums.size();
while(i <= k) {
int rear = ls.back();
ls.pop_back();
ls.push_front(rear);
++i;
} int idx = ;
for(pt=ls.begin();pt!=ls.end();++pt) nums[idx++] = *pt;
}
};


C++ STL@ list 应用 (leetcode: Rotate Array)的更多相关文章

  1. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

  2. LeetCode Rotate Array 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

  3. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  4. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  5. [leetcode]Rotate Array

    in place交换 如果是k步,那么就是把后面k个放到前面了嘛. 我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来 对于AB我们要通过reverse操作得 ...

  6. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  7. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  8. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  9. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

随机推荐

  1. Unity3D开发之查找面板上某个脚本(包括Missing)

    原地址:http://blog.csdn.net/lihandsome/article/details/24265411 有时候我们需要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相 ...

  2. Servlet课程0425(七) 到数据库中去验证用户,同时防止SQL注入漏洞

    Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...

  3. Android:仿微信开场切换界面

    这实例很多人仿做,好实例还是不容错过!最重要是素材容易拿~ 效果: 默认3页面的切换,最后一个页面带按钮,点击进入另外一个页面 思路: 1.准备5个布局页面,1个为主函数布局页面,3个为切换的页面(其 ...

  4. Android:使用ViewPager实现左右滑动切换图片(图上有点点)

    在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...

  5. 【iOS开发】iOS7 兼容及部分细节

    1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...

  6. WinAPI——钩子函数大全3

    函数原形:LRESULT CALLBACK JournalPlaybackProc(int code, WPARAM wParam, LPARAM lParam); 参数: code:指示一个代码,被 ...

  7. 【HDOJ】1699 The comment in cpp

    注意测试数据12/*hduacm// abcd结果是1/*hduacm// ABCD /* 1699 */ #include <iostream> #include <sstream ...

  8. NPOI的版本查看

    从github上clone源代码 git clone https://github.com/tonyqus/npoi.git 下载的版本库中,有一个名为Release Notes.txt的文件,在这个 ...

  9. SQLite支持的SQL数据操作

    事务处理 Posted on 2013 年 1 月 1 日 by 林溪   事务为一组SQL命令的集合,这些SQL命令在执行时不可进行分割,即要么全部执行这些SQL命令,要么一个都不进行执行,事务操作 ...

  10. [swustoj 1092] 二分查找的最大次数

    二分查找的最大次数(1092) 问题描述 这里是一个在排序好的数组A(从小到大)中查找整数X的函数,返回值是查找次数. int binarySearch(inta[],int n,int x)//数组 ...