C++ list用法
创建一个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用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
- [转]thinkphp 模板显示display和assign的用法
thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...
随机推荐
- PHPCMS v9 模型概念及用法
phpcms v9 模型概念 一.什么是模型? 模型是系统知识的抽象表示.我们不能仅仅通过语言来描述一个系统,也不能仅仅通过记忆来记录关于系统的知识.知识是通过某种媒介来表达的,这种媒介所表达的内容就 ...
- setTintColor
[self.button:[UIColor whiteColor]]; 初步研究 这个是决定的 btn的 外边框的颜色
- c# 6.0 学习笔记
refer : http://www.cnblogs.com/yinrq/p/5600530.html http://www.cnblogs.com/wolf-sun/p/5168217.html h ...
- A basic Windows service in C++ (CppWindowsService)
A basic Windows service in C++ (CppWindowsService) This code sample demonstrates creating a basic Wi ...
- BaseFragment
public abstract class BaseFragment extends Fragment { public FragmentActivity mActivity; /** * 此方法可以 ...
- 点击类名方法名如何连接到相应的Android源代码
. 找到Android SDK所在路径 “D:\androidDev\android-sdk-windows\” . 在该目录下选择platforms到 “D:\androidDev\android- ...
- linux grep shell相关
http://blog.csdn.net/buutterfly/article/details/6615162 http://www.2cto.com/os/201307/224496.html ht ...
- COJ 0138 NOIP201108计算系数
NOIP201108计算系数 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个多项式(ax + by)^k,请求出多项式 ...
- 【转】Thunderbird on Ubuntu 12.04 – 调整邮件列表行间距
原文网址:http://www.xuebuyuan.com/414703.html markz@markz-hp6200:~$ cd .thunderbird/ markz@markz-hp6200: ...
- HDOJ(HDU) 1465 不容易系列之一(错排)
Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就 ...