C++ Standard Library

一、特点

并没怎么用面向对象的特性。

For efficiency reasons, STL is not object-oriented:

    • Makes little use of inheritance, and
    • Makes no use of virtual functions

 

二、分类

STL是c++的精华,开始前先在此提一下。

有助于总体理解的链接:[C++ STL] 各容器简单介绍

The Standard Template Library (STL) is a part of the C++ standard library, Some others are:

分类一

    • The C standard library
    • Language support: <exception>, <ctime>, ...
    • Strings: <string>, <cstring>, ...
    • Numerics: <cmath>, <complex>, ...
    • I/O: <iostream>, <fstream>, <cstdio>, ...
    • Exception classes
    • Smart pointers
    • Classes for internationalisation support

 

分类二

    • Containers: vector, list, stack, ...
    • Algorithms: find, sort, copy, ...
    • Iterators are the glue between the two!

 

 

Containers 蓝图

一、c++11新特性

四大板块 

[c++] Sequence Containers

In reality, not all operations are supported in all containers, 

More details: http://www.cplusplus.com/reference/stl/

 

适配器 Adaptors

适配器?Adaptors和序列容器的关系是什么?

本质上,适配器是使一事物的行为类似于另一类事物的行为的一种机制。

容器适配器让一种已存在的容器类型采用另一种不同的抽象类型的工作方式实现。

例如,stack适配器可使任何一种顺序容器以栈的方式工作。

 

二、效率对比

[c++] Sequence Containers

需要逐个实践下,为下一节做准备 

[c++] Sequence Containers

 

 

 

 

容器解密


Vector

一、插入操作

resize() 

Ref: 关于vector的resize()的理解

resize()的作用是改变vector中元素的数目。

如果n比当前的vector元素数目要小,vector的容量要缩减到resize的第一个参数大小,既n。并移除那些超出n的元素同时销毁他们。

如果n比当前vector元素数目要大,在vector的末尾扩展需要的元素数目,如果第二个参数val指定了,扩展的新元素初始化为val的副本,否则按类型默认初始化。

注意:如果n大于当前的vector的容量(是容量,并非vector的size),将会引起自动内存分配。所以现有的pointer, references, iterators将会失效。

 

综合操作示范

int func_vector_insert(void)
{
  cout << "== vector insert() show ==" << endl;

  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );  //某位置插入一个数字
  for (auto p = myvector.begin(); p != myvector.end(); p++)
  {
      std::cout << (*p) << ", ";
  }
  std::cout << std::endl;

  myvector.insert (it,2,300);  //某位置插入若干数字

  for (auto p = myvector.begin(); p != myvector.end(); p++)
  {
      std::cout << (*p) << ", ";
  }
  std::cout << std::endl;

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2, anothervector.begin(), anothervector.end());  //某位置插入一段动态同类的数字

  for (auto p = myvector.begin(); p != myvector.end(); p++)
  {
      std::cout << (*p) << ", ";
  }
  std::cout << std::endl;

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);  //某位置插入一段静态同类的数字

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

  return 0;
}

//******************************************************************************

void func_vector(void)
{
//    vector<int> vec= new vector();
    vector<int> vec= {1, 2, 3, 4, 5};
    cout << vec.at(0) << endl;
    cout << "size = " << vec.size() << endl;
    cout << "capacity = " << vec.capacity() << endl;

    cout << "resize(10)\n";
    vec.resize(10);

    /* Jeff --> I don't like unsigned here. */
//    for (unsigned int i = 0; i < vec.size(); i++)
//    {
//        cout << vec.at(i) << "\n";
//    }

    for (auto p = vec.begin(); p != vec.end(); p++)
    {
        // Jeff --> endl == \n + flush
        cout << (*p) << ' ';
    }
    cout << endl;

    cout << "size = " << vec.size() << endl;
    cout << "capacity = " << vec.capacity() << endl;

    cout << "vec.clear()" << endl;
    vec.clear();

    cout << "size = " << vec.size() << endl;
    cout << "capacity = " << vec.capacity() << endl;

//-----------------------------------------------------------------------------
    for (int i = 1; i <= 10; i++)
    {
        vec.push_back(i);
    }
    
    // Jeff --> vector<int>::iterator iter = vec.begin();
    // 可使用通用的iterator方式,毕竟list不能直接l.begin()+1这么用!
    cout << "erase(0-2)" << endl;
    vec.erase(vec.begin(), vec.begin() + 3);

    for (auto p = vec.begin(); p != vec.end(); p++)
    {
        // Jeff --> endl == \n + flush
        cout << (*p) << ' ';
    }
    cout << endl;

    cout << "size = " << vec.size() << endl;
    cout << "capacity = " << vec.capacity() << endl;

//-----------------------------------------------------------------------------

    func_vector_insert();

//-----------------------------------------------------------------------------

    cout << "vec.front() = " << vec.front() << endl;

    cout << "push_back(111)" << endl;
    vec.push_back(111);

    for (auto p = vec.begin(); p != vec.end(); p++)
    {
        // Jeff --> endl == \n + flush
        cout << (*p) << ' ';
    }
    cout << endl;

    cout << "size = " << vec.size() << endl;
    cout << "capacity = " << vec.capacity() << endl;
}
常见的修改操作示范

相关文章:

  • 2021-11-29
  • 2021-07-23
  • 2021-11-25
  • 2021-12-24
  • 2021-07-12
  • 2021-11-25
  • 2022-01-29
  • 2022-12-23
猜你喜欢
  • 2021-11-30
  • 2021-12-23
  • 2021-09-16
  • 2021-08-02
  • 2021-08-20
  • 2021-05-05
  • 2022-12-23
相关资源
相似解决方案