创建一个list实例并赋值:

 // 创建实例以及赋值
#include <iostream>
#include <list>
using namespace std;
int main () {
//第一种,通过构造函数
int myints[] = {,,,,};
list<int> mylist1(myints, myints+);
list<int> mylist2(,); // 2个值为100的元素
//第二种,用push_back,或push_front
for (int i = ; i <= ; ++i) mylist1.push_back(i);
mylist2.push_front ();
mylist2.push_front ();
//第三种,用assign
list<int> first;
list<int> second;
first.assign(,); // 给first添加7个值为100的元素
second.assign(first.begin(), first.end()); // 复制first给second
int myints[] = {, , };
first.assign (myints, myints + ); // 将数组myints的内容添加给first //第四种,见insert函数
return ;
}

成员函数:

Iterator:  (可用于遍历list)

iterator begin();  //返回指向第一个元素的迭代器

iterator end();  //返回指向最后一个元素的迭代器

reverse_iterator rbegin();  //返回指向第一个元素的逆向迭代器

reverse_rend();  //返回指向最后一个元素的逆向迭代器

 //list的遍历
#include <iostream>
#include <list>
using namespace std;
int main () {
int myints[] = {,,,,};
list<int> mylist (myints,myints+);
cout << "mylist contains:";
//这是正序输出:
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; list.clear();
//逆序输出:
for (int i = ; i <= ; ++i) mylist.push_back(i);
cout << "mylist backwards:";
for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)
cout << ' ' << *rit;
cout << '\n';
return ;
} 输出结果为:
mylist contains: 75 23 65 42 13
mylist backwards: 5 4 3 2 1

Capacity: (用于获取list容器大小信息)

bool empty() const;  //list为空时返回true

size_type size() const;  //返回list容器里元素的个数

size_type max_size() const;  //返回list容器最大能容纳的元素的个数,主要用于调用list的resize()函数时,检查所请求的size大小是否被允许

Element access:(用于获取首尾元素)

reference front();  //返回第一个元素的引用

const_reference front() const;

reference back();  //返回最后一个元素的引用

const_reference front() const;

Modifiers:

  • asign  //给容器添加新内容:

template<class InputIterator>

void assign(InputIterator first, InputIterator last);  //first,last是一个序列中起始和结束的迭代器的值,[first, last)包含了序列中所有元素

void assign(size_type n, const value_type& val);  //给list赋值n个值为val的元素

 // list::assign
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> first;
list<int> second;
first.assign(,); // 给first添加7个值为100的元素
second.assign(first.begin(), first.end()); // 复制first给second int myints[] = {, , };
first.assign (myints, myints + ); // 将数组myints的内容添加给first cout << "Size of first: " << int (first.size()) << '\n';
cout << "Size of second: " << int (second.size()) << '\n';
return ;
}
输出结果为:
Size of first: 3
Size of second:
  • push_front, pop_front, push_back, pop_back

void push_front(const value_type& val);  //在list头添加元素

void pop_front();  //删除list头的元素

void push_back(const value_type& val);  //在list尾添加元素

void pop_back();  //删除list尾的元素

 #include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist (,); // 2个值为100的元素
// list::push_front
mylist.push_front ();
mylist.push_front (); cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; // list::pop_front
cout << "Popping out the elements in mylist:";
while (!mylist.empty()) {
cout << ' ' << mylist.front();
mylist.pop_front();
}
cout << "\nFinal size of mylist is " << mylist.size() << '\n'; // list::push_back
int myint;
cout << "Please enter some integers (enter 0 to end):\n";
do {
cin >> myint;
mylist.push_back (myint);
} while (myint);
cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; // list::pop_back
while (!mylist.empty()) {
cout << ' ' << mylist.back();
mylist.pop_back();
}
cout << "\nFinal size of mylist is " << mylist.size() << '\n'; return ;
}
输出结果:
mylist contai: 300 200 100 100
Popping out the elements in mylist: 300 200 100 100
Final size of mylist is
Please enter some integers (enter 0 to end):
56 23 8 5 6 0
mylist contains: 56 23 8 5 6 0
0 6 5 8 23 56
Final size of mylist is
  • insert  //插入元素:

iterator insert (iterator position, const value_type& val);  //position是要插入的这个list的迭代器,val是要插入的值

void insert (iterator position, size_type n, const value_type& val);  //从该list容器中的position位置处开始,插入n个值为val的元素

template <class InputIterator>

void insert (iterator position, InputIterator first, InputIterator last);  //first,last是我们选择的把值插入到这个list中的值所在的容器的迭代器

 // inserting into a list
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main () {
list<int> mylist;
list<int>::iterator it;
// 初始化
for (int i = ; i <= ; ++i) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // 迭代器it现在指向数字2 ^
//在i0t指向的位置出插入元素10
mylist.insert (it,); // 1 10 2 3 4 5 // "it" 仍然指向数字2 ^
//在it指向的位置出插入两个元素20
mylist.insert (it,,); // 1 10 20 20 2 3 4 5 --it; // 现在it指向数字20 ^ vector<int> myvector (,); //创建vector容器,并初始化为含有2个值为30的元素
//将vector容器的值插入list中
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
//it仍然指向数字20 // ^
cout << "mylist contains:";
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 1 10 20 30 30 20 2 3 4 5
  • erase  //删除元素:

iterator erase (iterator position);  //删除迭代器position指向的值,也可以不用变量接收其返回值

iterator erase (iterator first, iterator last);  //删除[first, last)中的值,也可以不用变量接收其返回值

 // erasing from list
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist;
list<int>::iterator it1,it2; // set some values:
for (int i = ; i < ; ++i) mylist.push_back(i*); // 10 20 30 40 50 60 70 80 90
it1 = it2 = mylist.begin(); // ^^
advance (it2,); // ^ ^
++it1; // ^ ^ it1 = mylist.erase (it1); // 10 30 40 50 60 70 80 90
// ^ ^ it2 = mylist.erase (it2); // 10 30 40 50 60 80 90
// ^ ^ ++it1; // ^ ^
--it2; // ^ ^
//没有变量接收其返回值
mylist.erase (it1,it2); // 10 30 60 80 90
// ^
cout << "*it1 : " << *it1 << endl;
cout << "mylist contains:";
for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
cout << ' ' << *it1;
cout << '\n'; return ;
}
输出结果:
it1 : 40
mylist contains: 10 30 60 80 90
  • swap  //交换两个list的内容

void swap(list& x);  //要交换的两个列表的存储的元素的类型必须是一样的,列表大小可以不同

 // swap lists
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> first (,); // 三个值为100的元素
list<int> second (,); // 五个值为200的元素 first.swap(second); cout << "first contains:";
for (list<int>::iterator it = first.begin(); it != first.end(); it++)
cout << ' ' << *it;
cout << '\n'; cout << "second contains:";
for (list<int>::iterator it = second.begin(); it != second.end(); it++)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
first contains: 200 200 200 200 200
second contains: 100 100 100
  • resize  //调整list大小

void resize (size_type n, value_type val = value_type());  //将list大小调整为能容纳n个元素,若n大于当前list大小,则会从list末尾一直插入val值,直到list大小满足n;

 // resizing list
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist; // 初始化
for (int i = ; i < ; ++i) mylist.push_back(i); mylist.resize();
mylist.resize(,);
mylist.resize(); cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 1 2 3 4 5 100 100 100 0 0 0 0
  • clear  //清空list

void clear();  //删除list的所有元素

 // clearing lists
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist;
list<int>::iterator it; mylist.push_back ();
mylist.push_back ();
mylist.push_back (); cout << "mylist contains:";
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.clear();
mylist.push_back ();
mylist.push_back (); cout << "mylist contains:";
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 100 200 300
mylist contains: 1101 2202

Operations:

  • splice  //将一个list中的值移到另一个list中

void splice (iterator position, list& x);  //将列表x中的所有元素移到当前list中,从当前列表的position指向的位置开始,此时列表x为空

void splice (iterator position, list& x, iterator i);  //将列表x中迭代器 i 指向的元素移到当前list的position指向的位置处,由于i指向的元素从列表x中被移除,所以迭代器 i 此时是invalid的;position是当前列表的迭代器,i是列表x的迭代器

void splice (iterator position, list& x, iterator first, iterator last);  //将列表x中[first, last)的元素移到当前list中,从position指向的位置开始;first, last是列表x的迭代器

 // splicing lists
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist1, mylist2;
list<int>::iterator it; // 初始化
for (int i = ; i <= ; ++i)
mylist1.push_back(i); // mylist1: 1 2 3 4 for (int i = ; i <= ; ++i)
mylist2.push_back(i*); // mylist2: 10 20 30 it = mylist1.begin();
++it; // 指向数字2 mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" 仍然指向数字2 mylist2.splice (mylist2.begin(),mylist1, it);
// mylist1: 1 10 20 30 3 4
// mylist2: 2
// "it" 此时已经无效了
it = mylist1.begin();
advance(it,); // "it" 指向数字30 mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
// mylist1: 30 3 4 1 10 20 cout << "mylist1 contains:";
for (it = mylist1.begin(); it != mylist1.end(); ++it)
cout << ' ' << *it;
cout << '\n'; cout << "mylist2 contains:";
for (it = mylist2.begin(); it != mylist2.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist1 contains: 30 3 4 1 10 20
mylist2 contains:
  • remove  //删除list中特定的值

void remove (const value_type& val);  //从list中删除所有值为val的元素

 // remove from list
#include <iostream>
#include <list>
using namespace std;
int main () {
int myints[]= {, , , , };
list<int> mylist (myints,myints+); mylist.remove(); cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 17 7
  • remove_if  //按条件删除

template <class Predicate>

void remove_if (Predicate pred);  //pred可以是一个函数,也可以是一个class,但它需要有一个参数,且参数类型跟list中存储元素类型相同,满足条件就返回true

 // list::remove_if
#include <iostream>
#include <list>
using namespace std;
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value < ); } // a predicate implemented as a class:
struct is_odd {
//重载操作符 ()
bool operator() (const int& value) { return (value % ) == ; }
}; int main () {
int myints[] = {, , , , , , , };
list<int> mylist (myints, myints + ); // 15 36 7 17 20 39 4 1 mylist.remove_if (single_digit); // 15 36 17 20 39 mylist.remove_if (is_odd()); // 36 20 cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 36 20
  • unique  //删除重复值

void unique();  //只能删除相邻的重复元素,然后保留第一个值,因此这个函数只对排好序的list有用

template <class BinaryPredicate>

void unique (BinaryPredicate binary_pred);  //binary_pred可以是函数,也可以是class,但它需要有两个参数,且类型跟list中存储的值类型相同,满足某个条件就返回true

 // list::unique
#include <iostream>
#include <cmath>
#include <list>
using namespace std;
// a binary predicate implemented as a function:
bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); } // a binary predicate implemented as a class:
struct is_near {
bool operator() (double first, double second) { return (fabs(first-second)<5.0); }
}; int main () {
double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
list<double> mylist (mydoubles,mydoubles+);
cout << "mylist contains:";
for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.unique();
cout << "mylist contains:";
for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35 mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
cout << "mylist contains:";
for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.unique (same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0
cout << "mylist contains:";
for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.unique (is_near()); // 2.72, 12.15, 72.25 cout << "mylist contains:";
for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25
mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25
mylist contains: 2.72 3.14 12.15 12.77 15.3 72.25 73 73.35
mylist contains: 2.72 3.14 12.15 15.3 72.25 73
mylist contains: 2.72 12.15 72.25

  • merge  //合并有序的list

void merge(list &x);  //会将列表x中的元素按默认的顺序移入当前列表当中,此时列表x为空,当前列表仍为有序列表

template<class Compare>

void merge (list& x, Compare comp);  //comp可以为一个函数,要求有两个参数且参数类型跟list中存储的元素类型相同,当满足条件时返回true,merge就按照这个条件将两个列表合并

 // list::merge
#include <iostream>
#include <list>
using namespace std;
// compare only integral part:
bool mycomparison (double first, double second) { return ( (first)<(second) ); } int main () {
list<double> first, second; first.push_back (3.1);
first.push_back (2.2);
first.push_back (2.9); second.push_back (3.7);
second.push_back (7.1);
second.push_back (1.4); first.sort();
second.sort(); first.merge(second);
cout << "first contains:";
for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
cout << ' ' << *it;
cout << '\n';
// (second 现在为空) second.push_back (2.1);
second.push_back(2.5); first.merge(second,mycomparison);
cout << "first contains:";
for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
first contains: 1.4 2.2 2.9 3.1 3.7 7.1
first contains: 1.4 2.1 2.2 2.5 2.9 3.1 3.7 7.1
  • sort  //排序

void sort();  //默认升序排列

template <class Compare>

void sort (Compare comp);  //comp可以是一个函数,要求有两个参数,类型跟list中元素类型相同,满足条件时返回true,sort()函数就按照comp中制定的规则对元素进行排序

 // list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>
using namespace std;
// comparison, not case sensitive.
bool compare_nocase (const string& first, const string& second) {
unsigned int i = ;
while ((i < first.length()) && (i < second.length()) ) {
//将大写字母转为小写字母
if (tolower(first[i]) < tolower(second[i])) return true;
else if (tolower(first[i]) > tolower(second[i])) return false;
++i;
}
return ( first.length() < second.length() );
} int main () {
list<string> mylist;
list<string>::iterator it;
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three"); mylist.sort(); cout << "mylist contains:";
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; mylist.sort(compare_nocase); cout << "mylist contains:";
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: Three one two
mylist contains: one Three two
  • reverse  //逆序:

void reverse();  //将list中元素的顺序逆转过来

 // reversing list
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist; for (int i = ; i < ; ++i) mylist.push_back(i); mylist.reverse(); cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n'; return ;
}
输出结果:
mylist contains: 9 8 7 6 5 4 3 2 1

Observers:

  • get_allocator  //返回一个跟该list有关的分配器对象

allocator_type get_allocator() const;  //可以用来给数组动态分配空间

 // list::get_allocator
#include <iostream>
#include <list>
using namespace std;
int main () {
list<int> mylist;
int * p; // allocate an array of 5 elements using mylist's allocator:
p = mylist.get_allocator().allocate(); // assign some values to array
for (int i = ; i < ; ++i) p[i] = i; cout << "The allocated array contains:";
for (int i = ; i < ; ++i) cout << ' ' << p[i];
cout << '\n'; mylist.get_allocator().deallocate(p,); return ;
}
输出结果:
The allocated array contains: 9 8 7 6 5 4 3 2 1

C++ list用法的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

  10. [转]thinkphp 模板显示display和assign的用法

    thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...

随机推荐

  1. 为Linux服务器伪装上Windows系统假象

    网络上的计算机很容易被黑客利用工具或其它手段进行扫描,以寻找系统中的漏洞,然后再针对漏洞进行攻击. 通过伪装Linux系统,给黑客设置系统假象,可以加大黑客对系统的分析难度,引诱他们步入歧途,从而进一 ...

  2. Nvidia显卡怎样查看显存大小及硬件相关信息

    在电脑上安装Nvidia显卡驱动,平时也会通过Nvidia控制面板来查看显示显存位宽及宽带.显示显存容量和显示显存芯片信息等等,那么该如何查看Nvidia显存大小以及Nvidia硬件相关信息呢? 1. ...

  3. jdk8永久代从方法区移除的验证

    /*** 测试使用jdk8中是否仍然可以使用永久代* jvm options * -Xms20m -Xmx20m -Xmn10m -XX:PermSize=10m -XX:MaxPermSize=10 ...

  4. 如何打一手好Log(转)

    如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...

  5. 【转】Java 多线程(四) 多线程访问成员变量与局部变量

    原文网址:http://www.cnblogs.com/mengdd/archive/2013/02/16/2913659.html 先看一个程序例子: public class HelloThrea ...

  6. ActionResult 的返回类型

    大多数操作方法会返回从 ActionResult 中派生的类的实例. ActionResult 类是所有操作结果的基础. 不过,也存在不同的操作结果类型,具体取决于操作方法执行的任务. 例如,最常见的 ...

  7. 通过并行 提高批量审核PDF性能

    上一篇文章提到了 通过 iTextSharp 实现PDF 审核盖章 ,如果当需要一次审核大批量的PDF我们如何来提高程序的性能呢? 下面我们通过并行计算来提升性能. 首先是一个审核PDF的方法 pub ...

  8. Android ImageView的scaletype属性

    ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType).android:scaleType是控制图片如何 ...

  9. HDU_1241——石油探索DFS

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  10. HDU_2042——递归反推

    Problem Description 你活的不容易,我活的不容易,他活的也不容易.不过,如果你看了下面的故事,就会知道,有位老汉比你还不容易.重庆市郊黄泥板村的徐老汉(大号徐东海,简称XDH)这两年 ...