#STL #List 容器函数以及一些内置函数的用法


首先是list

运行网页

点这里复制过去运行

list

原网址

定义

// constructing lists
#include <iostream>
#include <list>

int main ()
{
  // constructors used in the same order as described above:
  std::list<int> first;                                // empty list of ints
  std::list<int> second (4,100);                       // four ints with value 100
  std::list<int> third (second.begin(),second.end());  // iterating through second
  std::list<int> fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  //可以得到某函数里的元素,其中sizeof(myints)要除以sizeof(int),或者手动写myints + 5 也可以。反正要保证长度正确。
  int myints[] = {16,2,77,29};
  //sizeof求字节长度
  std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are: ";
  for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
    std::cout << *it << ' ';

  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

The contents of fifth are: 16 2 77 29 

begin(),end()

看看这个

// list::begin
#include <iostream>
#include <list>

int main ()
{
  int myints[] = {75,23,65,42,13, 21, 65, 94};
  //这个定义方式是得到myints里面的前五个元素
  std::list<int> mylist (myints,myints+5);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

mylist contains: 75 23 65 42 13

rbegin(),rend()

再看看这个,因为list是双向的列表,所以可以从头搜到尾,也可以从尾搜到头,rbegin()是尾,rend()是头。

// list::rbegin/rend
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  for (int i=1; i<=5; ++i) mylist.push_back(i);

  std::cout << "mylist backwards:";
  for (std::list<int>::reverse_iterator rit=mylist.rbegin(); rit!=mylist.rend(); ++rit)
    std::cout << ' ' << *rit;

  std::cout << '
';

  return 0;
}

运行结果

mylist backwards: 5 4 3 2 1

size();

// list::size
#include <iostream>
#include <list>

int main ()
{
//开始时空的
  std::list<int> myints;
  std::cout << "0. size: " << myints.size() << '
';
  for (std::list<int>::iterator it = myints.begin(); it != myints.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
//这里加入了十个元素
  for (int i=0; i<10; i++) myints.push_back(i);
  std::cout << "1. size: " << myints.size() << '
';
  for (std::list<int>::iterator it = myints.begin(); it != myints.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  myints.insert (myints.begin(),10,100);
  std::cout << "2. size: " << myints.size() << '
';
  for (std::list<int>::iterator it = myints.begin(); it != myints.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  myints.pop_back();
  std::cout << "3. size: " << myints.size() << '
';
  for (std::list<int>::iterator it = myints.begin(); it != myints.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  return 0;
}

点这里复制过去运行
运行结果

0. size: 0


1. size: 10
0 1 2 3 4 5 6 7 8 9 

2. size: 20
100 100 100 100 100 100 100 100 100 100 0 1 2 3 4 5 6 7 8 9 

3. size: 19
100 100 100 100 100 100 100 100 100 100 0 1 2 3 4 5 6 7 8 

empty()

原网址

// list::empty
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  int sum (0);

  for (int i=1;i<=10;++i) mylist.push_back(i);
//mylist.empty()会返回是否为空
  while (!mylist.empty())
  {
     sum += mylist.front();
     mylist.pop_front();
  }

  std::cout << "total: " << sum << '
';
  
  return 0;
}

点这里复制过去运行
运行结果

total: 55

resize()

原网址

改变大小
调整容器的大小,使其包含n个元素。

如果n小于当前容器的大小,则将内容减少到其前n个元素,并删除超出范围的元素(并销毁它们)。

如果n大于当前容器的大小,则通过在末尾插入所需数量的元素来扩展内容,以达到n的大小。
如果指定了val,则将新元素初始化为val的副本,否则,将对它们进行值初始化。

请注意,此函数通过插入或擦除容器中的元素来更改容器的实际内容。
// resizing list
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;

  // set some initial content:
  //输入随便十个数
  for (int i=1; i<10; ++i) mylist.push_back(i);
  //以下是输出当前链表状态
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  mylist.resize(5);
  //以下是输出当前链表状态
  std::cout << "mylist.resize(5);" << '
';
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  mylist.resize(8,100);
  //以下是输出当前链表状态
  std::cout << "mylist.resize(8,100);" << '
';
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

  mylist.resize(12);
  //以下是输出当前链表状态
  std::cout << "mylist.resize(12);" << '
';
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';


  std::cout << "mylist contains:" << '
';
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

1 2 3 4 5 6 7 8 9 

mylist.resize(5);
1 2 3 4 5 

mylist.resize(8,100);
1 2 3 4 5 100 100 100 

mylist.resize(12);
1 2 3 4 5 100 100 100 0 0 0 0 

mylist contains:
 1 2 3 4 5 100 100 100 0 0 0 0

front(), back()

原网址

返回对列表容器中第一个元素的引用。

与成员list :: begin返回一个迭代器到同一元素不同,该函数返回直接引用。

在空容器上调用此函数会导致未定义的行为。
// list::front
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;

  mylist.push_back(77);
  mylist.push_back(22);

  // now front equals 77, and back 22

  mylist.front() -= mylist.back();

  std::cout << "mylist.front() is now " << mylist.front() << '
';

  return 0;
}

点这里复制过去运行
运行结果

mylist.front() is now 55

back()

原网址

返回对列表容器中最后一个元素的引用。

与成员list :: end不同,成员list :: end返回仅此元素之后的迭代器,此函数返回直接引用。

在空容器上调用此函数会导致未定义的行为。
// list::back
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;

  mylist.push_back(10);

  while (mylist.back() != 0)
  {
    mylist.push_back ( mylist.back() -1 );
  }

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end() ; ++it)
    std::cout << ' ' << *it;

  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

mylist contains: 10 9 8 7 6 5 4 3 2 1 0

assign()

原网页
重新分配;也可以看做是重新定义整个list,包括里面的元素以及长度。
主要格式就是:
1、将另一个list b 复制过来
a.assign(b.begin(), b.end());
2、将数组的某一段复制过来
a.assign (myints + 1, myints + 5);就是将第1个元素到第4个元素复制下来(数组首位坐标是0)。
a.assign (myints, myints + 5);就是将数组前五个元素复制下来。

// list::assign
#include <iostream>
#include <list>

int main ()
{
  std::list<int> first;
  std::list<int> second;

  first.assign (7,100);                      // 7 ints with value 100

  second.assign (first.begin(),first.end()); // a copy of first

  int myints[]={1776,7,4, 5, 67, 9563, 1241};
  first.assign (myints,myints+3);            // assigning from array

  //以下是输出当前链表状态
  std::cout << "first" << '
';
  for (std::list<int>::iterator it = first.begin(); it != first.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
  //以下是输出当前链表状态
  std::cout << "second" << '
';
  for (std::list<int>::iterator it = second.begin(); it != second.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
  second.assign (myints,myints+6);
  //以下是输出当前链表状态
  std::cout << "second" << '
';
  for (std::list<int>::iterator it = second.begin(); it != second.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
  second.assign (myints, myints + 3); // a copy of first
  //以下是输出当前链表状态
  std::cout << "second" << '
';
  for (std::list<int>::iterator it = second.begin(); it != second.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
  return 0;
}

点这里复制过去运行
运行结果

first
1776 7 4 

second
100 100 100 100 100 100 100 

second
1776 7 4 5 67 9563 

second
1776 7 4 

push_front()、pop_front()、push_back()、pop_back()

这四个我就不多解释了……分别是在表头插入,弹出表头,在表尾插入,弹出表尾。

insert()

原网页
这个插入可好玩了。
自行体会:

// inserting into a list
#include <iostream>
#include <list>
#include <vector>

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
  //以下是输出当前链表状态
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';


  it = mylist.begin();
                                                // 1 2 3 4 5
  ++it;       // it points now to number 2           ^
  std::cout << "it points now to number 2 " << '
' << '
';

  mylist.insert (it,10);                        // 1 10 2 3 4 5
  //以下是输出当前链表状态
  std::cout << "mylist.insert (it,10); " << '
';
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

                                                // 1 10 2 3 4 5
  // "it" still points to number 2                      ^
  mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5
  //                                                          ^ 
  //以下是输出当前链表状态
  std::cout << "it still points to number 2 " << '
' << '
';
  std::cout << "mylist.insert (it,2,20); " << '
';
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';

                                               // 1 10 20 20 2 3 4 5
  --it;       // it points now to the second 20            ^
  std::cout << "it points now to the second 20   " << '
'<< '
';
  std::cout << "std:: vector<int> myvector (2,30);" << '
';
  std::vector<int> myvector (2,30);
  //以下是输出当前链表状态
  for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';
  mylist.insert (it,myvector.begin(),myvector.end());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
    std::cout << "mylist.insert (it,myvector.begin(),myvector.end());" << '
';
  //以下是输出当前链表状态
  for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    std::cout << *it << ' ';
    std::cout << '
'<< '
';


  return 0;
}

点这里复制过去运行
运行结果

1 2 3 4 5 

it points now to number 2 

mylist.insert (it,10); 
1 10 2 3 4 5 

it still points to number 2 

mylist.insert (it,2,20); 
1 10 20 20 2 3 4 5 

it points now to the second 20   

std:: vector<int> myvector (2,30);
30 30 

mylist.insert (it,myvector.begin(),myvector.end());
1 10 20 30 30 20 2 3 4 5 

erase()

自行体会,这个注释已经写得很明白了。
原网页

// erasing from list
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it1,it2;

  // set some values:
  for (int i=1; i<10; ++i) mylist.push_back(i*10);

                              // 10 20 30 40 50 60 70 80 90
  it1 = it2 = mylist.begin(); // ^^
  advance (it2,6);            // ^                 ^
  ++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
                              //        ^

  std::cout << "mylist contains:";
  for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
    std::cout << ' ' << *it1;
  std::cout << '
';

  return 0;
}

这个的运行结果意义不大,看看注释就明白了。

swap()

// swap lists
#include <iostream>
#include <list>

int main ()
{
  std::list<int> first (3,100);   // three ints with a value of 100
  std::list<int> second (5,200);  // five ints with a value of 200

  first.swap(second);

  std::cout << "first contains:";
  for (std::list<int>::iterator it=first.begin(); it!=first.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '
';

  std::cout << "second contains:";
  for (std::list<int>::iterator it=second.begin(); it!=second.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

first contains: 200 200 200 200 200 
second contains: 100 100 100 

clear()

一键清空!!!!

// clearing lists
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it;

  mylist.push_back (100);
  mylist.push_back (200);
  mylist.push_back (300);

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  mylist.clear();
  mylist.push_back (1101);
  mylist.push_back (2202);

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

点这里复制过去运行
运行结果

mylist contains: 100 200 300
mylist contains: 1101 2202

splice()

这个更有意思,是将某一链表中的某一段直接截下来接到另一个链表上面去(被截掉的那部分会消失)。

// splicing lists
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist1, mylist2;
  std::list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=4; ++i)
     mylist1.push_back(i);      // mylist1: 1 2 3 4

  for (int i=1; i<=3; ++i)
     mylist2.push_back(i*10);   // mylist2: 10 20 30

  it = mylist1.begin();
  ++it;                         // points to 2
                //目标位置,要拼接的链表
  mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
                                // mylist2 (empty)
                                //it会跟着2
                                // "it" still points to 2 (the 5th element)
                 //拼接的起始位置,来源,在来源链表上的位置                         
  mylist2.splice (mylist2.begin(),mylist1, it);
                                // mylist1: 1 10 20 30 3 4
                                // mylist2: 2
                                //但是it不会跟着2来到另一个链表,它无法离开原链表,它会直接失效。
                                // "it" is now invalid.
  it = mylist1.begin();
  std::advance(it,3);           // "it" points now to 30,也就是it重新指向第三个元素30
                   //目标起始位置,来源链表,来源链表上的位置的起点,和终点
  mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
                                // mylist1: 30 3 4 1 10 20
                                
//mylist1包含:30 3 4 1 10 20
  std::cout << "mylist1 contains:";
  for (it=mylist1.begin(); it!=mylist1.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';
  
//mylist2包含:2
  std::cout << "mylist2 contains:";
  for (it=mylist2.begin(); it!=mylist2.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

reverse()

反转列表容器中元素的顺序。

// reversing list
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;

  for (int i=1; i<10; ++i) mylist.push_back(i);

  mylist.reverse();

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '
';

  return 0;
}

mylist包含:9 8 7 6 5 4 3 2 1

remove()

删除指定元素

// remove from list
#include <iostream>
#include <list>

int main ()
{
  int myints[]= {17,89,7,14};
  std::list<int> mylist (myints,myints+4);

  mylist.remove(89);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

Output:
mylist contains: 17 7 14

unique()

去重,目前只要记住如何去除重复元素就可以了,去重满足某种条件的那个用不到。

// list::unique
#include <iostream>
#include <cmath>
#include <list>

// 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 };
  std::list<double> mylist (mydoubles,mydoubles+10);
  
  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

  mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15
                                       // 15.3,  72.25, 73.0

  mylist.unique (is_near());           //  2.72, 12.15, 72.25

  std::cout << "mylist contains:";
  for (std::list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}


mylist contains: 2.72 12.15 72.25

merge()

合并!

// list::merge
#include <iostream>
#include <list>

// compare only integral part:
bool mycomparison (double first, double second)
{ return ( int(first)>int(second) ); }

int main ()
{
  std::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);
  //first contains: 1.4 2.2 2.9 3.1 3.7 7.1
  // (second is now empty)
//会自动排好序的,但是确实将插入的元素一个个地找到相对应的位置拍好,所以整个序列不一定是排好序的!
//只会找到第一个满足条件的位置放下
//这就是为什么上面要先sort()再merge();
  second.push_back (2.1);
//以下是自己重定义排序的方法
  first.merge(second,mycomparison);
//first contains: 2.1 1.4 2.2 2.9 3.1 3.7 7.1

  std::cout << "first contains:";
  for (std::list<double>::iterator it=first.begin(); it!=first.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

sort()

不解释……

// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>

// comparison, not case sensitive.
bool compare_nocase (int first, int second)
{
  return (first > second);
}

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it;
  mylist.push_back (1);
  mylist.push_back (2);
  mylist.push_back (3);

  mylist.sort();

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  mylist.sort(compare_nocase);

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '
';

  return 0;
}

………………
翻这些好累人啊,以后能看书就看书吧,要找详细的定义和用法再来核对一下吧,真的是有慢又累%&…………*&&…………

原文地址:https://www.cnblogs.com/yuanyulin/p/14026725.html