C++ List的使用


vector容器提供了對元素的快速隨機訪問,但付出的代價是在其任意位置插入和刪除元素,比在容器尾部插入和刪除的開銷更大。 而list容器可以在任何位置快速插入和刪除,但付出的代價是元素的隨機訪問開銷更大

構造List對象

頭文件: #include

list<int> b;           //無參數 - 構造一個空的list,
list<int> a(10);       //定義了10個整型元素的向量(尖括號中爲元素類型名,它可以是任何合法的數據類型),但沒有給出初值,其值是不確定的。
list<int> a(10, 1);     //定義了10個整型元素的向量,且給出每個元素的初值爲1
list<int> a(b);        //用b向量來創建a向量,整體複製性賦值, 拷貝構造
list<int> v3 = a;       //移動構造

基本操作-屬性獲取/調整

1 list.empty-判斷list是否爲空

a.empty()

2 list.size-元素的個數

a.size()

3 list.resize-調整大小

a.resize(10);        //將a的現有元素個數調至10個,多則刪,少則補,其值隨機
a.resize(10, 2);      //將a的現有元素個數調至10個,多則刪,少則補,其值爲2

4 比較操作符

a==b;     //b爲向量,向量的比較操作還有!=,>=,<=,>,<

基本操作-增加元素

1 list.push_back()-尾部插入元素

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

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

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    mylist.push_back (myint);
  } while (myint);

  std::cout << "mylist stores " << mylist.size() << " numbers.\n";

  return 0;
}

2 list.insert()-插入元素

第一個函數,在迭代器指定的位置前插入值爲x的元素

第二個函數,在迭代器指定的位置前插入n個值爲x的元素

a.insert(a.begin(), 5);         //在a的第1個元素(從第0個算起)的位置插入數值5,如a爲1,2,3,4,插入元素後爲1,5,2,3,4
a.insert(a.begin(), 3, 5);       //在a的第1個元素(從第0個算起)的位置插入3個數,其值都爲5

3 list.push_front()-頭部插入元素

a.push_front (200);

基本操作-刪除元素

1 list.erase-刪除

iterator erase (const_iterator position);//刪除指定位置元素,返回迭代器
//例如:a.erase(a.begin())

iterator erase (const_iterator first, const_iterator last);//刪除指定迭代器中間的元素,左閉右開
//例如:a.erase(a.begin(), a.end());


// 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 << '\n';

  return 0;
}

2 list.clear-清空list中的元素

將容器裏的內容清空,size值爲0,但是存儲空間沒有改變

a.clear();

3 list.pop_back-刪除尾部元素

a.pop_back();         //刪除a向量的最後一個元素

4 list.pop_front-刪除頭部元素

a.pop_front();

基本操作-查找/修改元素

1 list.assign-重新賦值

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};
first.assign (myints,myints+3); xxxxxxxxxx a.assign(4,2); 

2 list.back-返回list的最後一個元素

返回尾部元素的值,與end()函數有區別,back()函數返回的是尾部元素的迭代器

list.back()

3 list.front-返回list的第一個元素

返回第一個元素的值,與begin()函數有區別,begin()函數返回的是第一個元素的迭代器

list.front()

// listtor::operator[]
#include <iostream>
#include <listtor>

int main ()
{
  std::listtor<int> mylisttor (10);   // 10 zero-initialized elements

  std::listtor<int>::size_type sz = mylisttor.size();

  // assign some values:
  for (unsigned i=0; i<sz; i++) mylisttor[i]=i;

  // reverse listtor using operator[]:
  for (unsigned i=0; i<sz/2; i++)
  {
    int temp;
    temp = mylisttor[sz-1-i];
    mylisttor[sz-1-i]=mylisttor[i];
    mylisttor[i]=temp;
  }

  std::cout << "mylisttor contains:";
  for (unsigned i=0; i<sz; i++)
    std::cout << ' ' << mylisttor[i];
  std::cout << '\n';

  return 0;
}


4 swap-交換

交換這兩個容器的內容,這涉及到存儲空間的重新分配

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);

基本操作-迭代器

1 list.begin/list.end-迭代器

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


2 list.cbegin/list.cend-常量迭代器

返回一個類型爲cont::const_iterator 的對象

#include <iostream>
#include <List>

int main ()
{
 std::list<int> mylist = {5,10,15,20};
  std::cout << "mylist contains:";
  for (auto it = mylist.cbegin(); it != mylist.cend(); ++it)
    std::cout << ' ' << *it;

  return 0;
}


算法操作

1 list.sort()

2 list.reverse() -反轉

3 list.unique()-刪除list中重複的元素

4 list.splice()-合併兩個list

// 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" still points to 2 (the 5th element)
                                          
  mylist2.splice (mylist2.begin(),mylist1, it);
                                // mylist1: 1 10 20 30 3 4
                                // mylist2: 2
                                // "it" is now invalid.
  it = mylist1.begin();
  std::advance(it,3);           // "it" points now to 30

  mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
                                // mylist1: 30 3 4 1 10 20

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

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

  return 0;
}

5 list…merge合併兩個list

first.merge(second);

參考

http://www.cplusplus.com/reference/list/list/merge/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章