C++STL容器之vector

目录

1.查看refrence

2. Iterator invalidation(迭代器失效的情况)

3.Member types

4.Member functions

5.Element access

6.Iterators

7.Capacity

8.Modifiers

9.Non-member functions


1.查看refrence

cppreference

begin()和cbegin()异同

push_back&emplace_back

2. Iterator invalidation(迭代器失效的情况)

Operations(操作)Invalidated(失效场景)
All read only operationsNever
swap, std::swapend()
clear, operator=, assignAlways
reserve, shrink_to_fitIf the vector changed capacity, all of them. If not, none.
eraseErased elements and all elements after them (including end())
push_back, emplace_backIf the vector changed capacity, all of them. If not, only end().
insert, emplaceIf the vector changed capacity, all of them. If not, only those at or after the insertion point (including end()).
resizeIf the vector changed capacity, all of them. If not, only end() and any elements erased.
pop_backThe element erased and end().

3.Member types

Member typeDefinition
value_typeT
allocator_typeAllocator
size_typeUnsigned integer type (usually std::size_t)
difference_typeSigned integer type (usually std::ptrdiff_t)
referenceAllocator::reference(until C++11)
value_type&(since C++11)
const_referenceAllocator::const_reference(until C++11)
const value_type&(since C++11)
pointerAllocator::pointer(until C++11)
std::allocator_traits<Allocator>::pointer(since C++11)
const_pointerAllocator::const_pointer(until C++11)
std::allocator_traits<Allocator>::const_pointer(since C++11)
iteratorLegacyRandomAccessIterator to value_type
const_iteratorLegacyRandomAccessIterator to const value_type
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

4.Member functions

Member functions 
(constructor)constructs the vector
(public member function)
(destructor)destructs the vector
(public member function)
operator=assigns values to the container
(public member function)
assignassigns values to the container
(public member function)
get_allocatorreturns the associated allocator
(public member function)

5.Element access

ataccess specified element with bounds checking
(public member function)
operator[]access specified element
(public member function)
frontaccess the first element
(public member function)
backaccess the last element
(public member function)
data(C++11)direct access to the underlying array
(public member function)

6.Iterators

begincbegin

(C++11)

returns an iterator to the beginning
(public member function)

endcend

(C++11)

returns an iterator to the end
(public member function)

rbegincrbegin

(C++11)

returns a reverse iterator to the beginning
(public member function)

rendcrend

(C++11)

returns a reverse iterator to the end
(public member function)

7.Capacity

empty

checks whether the container is empty
(public member function)

size

returns the number of elements
(public member function)

max_size

returns the maximum possible number of elements
(public member function)

reserve

reserves storage
(public member function)

capacity

returns the number of elements that can be held in currently allocated storage
(public member function)

shrink_to_fit

(C++11)

reduces memory usage by freeing unused memory
(public member function)

8.Modifiers

clear

clears the contents
(public member function)

insert

inserts elements
(public member function)

emplace

(C++11)

constructs element in-place
(public member function)

erase

erases elements
(public member function)

push_back

adds an element to the end
(public member function)

emplace_back

(C++11)

constructs an element in-place at the end
(public member function)

pop_back

removes the last element
(public member function)

resize

changes the number of elements stored
(public member function)

swap

swaps the contents
(public member function)

9.Non-member functions

operator==

operator!=(removed in C++20)

operator<(removed in C++20)

operator<=(removed in C++20)

operator>(removed in C++20)

operator>=(removed in C++20)

operator<=>(C++20)

lexicographically compares the values in the vector
(function template)

std::swap(std::vector)

specializes the std::swap algorithm (function template)

erase(std::vector)(C++20)

erase_if(std::vector)(C++20)

Erases all elements satisfying specific criteria(function template)

10.应用举例

10.1 初始化,插入与遍历的一个小例子

// g++ 001-vector.cpp -std=c++11
#include <iostream>
#include <vector>
int main()
{
        //Create a vector containing integers
        std::vector<int> v = { 7, 5, 16, 8 };
        // Add two more integers to vector
        v.push_back(25);
        v.push_back(13);
        // Print out the vector
        std::cout << "v = { "; 
        for (int n : v) {
                std::cout << n << ", ";
        }
        std::cout << "}; \n";
}

10.2 使用reserve(size_type new_cap)函数优化内存分配

#include <cstddef>
#include <new>
#include <vector>
#include <iostream>
 
// minimal C++11 allocator with debug output
template <class Tp>
struct NAlloc {
    typedef Tp value_type;
    NAlloc() = default;
    template <class T> NAlloc(const NAlloc<T>&) {}
    /* 申请内存 */
    Tp* allocate(std::size_t n)
    {
        n *= sizeof(Tp);
        std::cout << "  allocating(申请内存) " << n << " bytes\n"; 
        return static_cast<Tp*>(::operator new(n));
    }
    /* 释放内存 */
    void deallocate(Tp* p, std::size_t n) 
    {
        std::cout << "deallocating(释放内存) " << n*sizeof*p << " bytes\n";
        ::operator delete(p);
    }
};
template <class T, class U>
bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; }
template <class T, class U>
bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; }
 
int main()
{
    int sz = 100;
    std::cout << "**********using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
        v1.reserve(sz);
        for(int n = 0; n < sz; ++n)
            v1.push_back(n);
    }
    std::cout << "\n\n\n====================================================================\n\n\n";
    std::cout << "**********not using reserve: \n";
    {
        std::vector<int, NAlloc<int>> v1;
        for(int n = 0; n < sz; ++n)
            v1.push_back(n);
    }
}

10.3 【push_back】VS【emplace_back】

(1)push_back直接构造,本质依然是需要先调用构造函数构造一个临时变量,再把这个临时变量拷贝构造进入vector的内存空间中;
(2)emplace_back没有构造临时对象,而是直接在vector的内存空间中原地构造.

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值