C++ vector 最详细教程2 (包含程序源码)

3.vector容量相关函数含义及操作

/size 函数 size_type size() const;max_size0
//函数返回vector的存储元素的个数,而不是其实际的容量

void testSize()
{
    std::vector<int> myints;
    std::cout << "0. size: " << myints.size() << '\n';
    for (int i = 0; i<10; i++)
        myints.push_back(i);

    std::cout << "1. size: " << myints.size() << '\n';
    myints.insert(myints.end(), 10, 100);
    std::cout << "2. size: " << myints.size() << '\n';
    myints.pop_back();
    std::cout << "3. size: " << myints.size() <<'\n' << myints.max_size() << '\n';

}

//size 函数  size_type size() const;max_size0
//函数返回vector的存储元素的个数,而不是其实际的容量

void myresize()
{
    std::vector<int> myvector;
    // set some initial content:
    for (int i = 1; i<10; i++) 
        myvector.push_back(i);
    myvector.resize(5);
    /*
    1、resize(n)调整容器的长度大小,使其能容纳n个元素。
       如果n小于容器的当前的size,则删除多出来的元素。
       否则,添加采用值初始化的元素。
    2、resize(n,t)
       多一个参数t,将所有新添加的元素初始化为t。
    */
    myvector.resize(8, 100);

    myvector.resize(12);
    std::cout << "myvector contains:";

    for (int i = 0; i<myvector.size(); i++)
        std::cout << ' ' << myvector[i];
    std::cout << '\n';

}


/*
capacity函数
size_type capacity() const;
在STL中,拥有capacity属性的容器只有vector和string
返回vector中实际分配的内存大小。示例如下:
*/

void mycapacity()
{
    std::vector<int> myvector;
    for (int i = 0; i<100; i++) 
        myvector.push_back(i);
    std::cout << "size: " << (int)myvector.size() << '\n';
    std::cout << "capacity: " << (int)myvector.capacity() << '\n';
    std::cout << "max_size: " << (int)myvector.max_size() << '\n';

}

/*与size capacity函数对应的两个函数如下
使用resize(),容器内的对象内存空间是真正存在的。
使用reserve()仅仅只是修改了capacity的值,容器内的对象并没有真实的内存空间(空间是"野"的)
  下面测试结果如下
v.size() == 0 v.capacity() = 0
v.size() == 0 v.capacity() = 10
v.size() == 11 v.capacity() = 15
*/

void res()
{
    std::vector<int> v;
    std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
    v.reserve(10);
    std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
    v.resize(10);
    v.push_back(0);
    std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl;
}

/*
void shrink_to_fit();
改变vector的容量使其和vector的尺寸大小符合
输出结果
1. capacity of myvector: 100
2. capacity of myvector: 100
3. capacity of myvector: 10

*/

void myshrink_to_fit()
{
    std::vector<int> myvector(100);
    std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n';
    myvector.resize(10);
    std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n';
    myvector.shrink_to_fit();
    std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n';
}

4.vector元素获取函数

  []操作符函数
  reference operator[] (size_type n);
  const_reference operator[] (size_type n) const;
  获取vector中元素,这个和C中获取数组元素一样

  at函数
  reference at(size_type n);
  const_reference at(size_type n) const;
  获取vector中的元素,这个和[]的作用一样,不过和[]不同的是,at()函数要对数组的边界进行检查, 如果越界就会抛出异常,但是[]不会。

  front函数
  reference front();
  const_reference front() const;
  返回vector中首个元素的引用

  back 函数
  reference back();
  const_reference back() const;
  返回vector中末尾元素的引用

  data 函数 C++11
  value_type* data() noexcept;
  const value_type* data() const noexcept;
5.vector元素修改

/* assign函数,在C++11中多了一个 初始化列表的函数 initializer list (3) void assign (initializer_list il); 函数重新设置vector的内容,替换之前的内容。

结果如下:
Size of first: 7
Size of second: 5
Size of third: 3
*/

void myassign()
{
    std::vector<int> first;
    std::vector<int> second;
    std::vector<int> third;
    first.assign(7, 100);             // 7 ints with a value of 100

    std::vector<int>::iterator it;
    it = first.begin() + 1;

    second.assign(it, first.end() - 1); // the 5 central values of first

    int myints[] = { 1776, 7, 4 };
    third.assign(myints, myints + 3);   // assigning from array.

    std::cout << "Size of first: " << int(first.size()) << '\n';
    std::cout << "Size of second: " << int(second.size()) << '\n';
    std::cout << "Size of third: " << int(third.size()) << '\n';

}

/* push_back 函数
void push_back (const value_type& val); void push_back (value_type&& val); (C++11)
在vector的末尾插入一个元素。

pop_back 函数
void pop_back(); 在vector的末尾删除一个元素 */

/*insert 函数
其中的move(4),initializer list(5) 是C++11中的函数
函数的作用就是在vector中插入一个或者多个元素,函数的返回值是指向插入新元素中的第一个的迭代器 */

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

    it = myvector.begin();
    it = myvector.insert(it, 200);
    myvector.insert(it, 2, 300);

    // "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());

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

}

/*
clear函数
void clear(); 清除vector中所有元素,然后vector的size大小变为0

erase 函数
iterator erase (iterator position); iterator erase (iterator first, iterator last);
函数移走vector中的一个或者一定范围内的元素。 */

void myerase()
{
    std::vector<int> myvector;
    // set some values (from 1 to 10)
    for (int i = 1; i <= 10; i++) myvector.push_back(i);
    // erase the 6th element
    myvector.erase(myvector.begin() + 5);
    // erase the first 3 elements:
    myvector.erase(myvector.begin(), myvector.begin() + 3);
    std::cout << "myvector contains:";
    for (unsigned i = 0; i<myvector.size(); ++i)
        std::cout << ' ' << myvector[i];
    std::cout << '\n';

}

/*
swap 函数 void swap (vector& x);
作用就是交换两个vector中的元素
*/

void myswap()
{
    std::vector<int> foo(3, 100);   // three ints with a value of 100
    std::vector<int> bar(5, 200);   // five ints with a value of 200
    foo.swap(bar);
    std::cout << "foo contains:";
    for (unsigned i = 0; i<foo.size(); i++)
        std::cout << ' ' << foo[i] <<std::endl;
    std::cout << "bar contains:";
    for (unsigned i = 0; i<bar.size(); i++)
        std::cout << ' ' << bar[i] << std::endl;
}

/*

/*

emplace 函数(C++11)
template

void myemplace()
{
    std::vector<int> myvector = { 10, 20, 30 };
    auto it = myvector.emplace(myvector.begin() + 1, 100);
    myvector.emplace(it, 200);
    myvector.emplace(myvector.end(), 300);
    std::cout << "myvector contains:";
    for (auto& x : myvector)
        std::cout << ' ' << x;
    std::cout << '\n';

}

/*

emplace_back 函数  

template <class... Args>
void emplace_back (Args&&... args);
在函数的末尾构造并插入一个新元素,这个函数和push_back函数类似,不同的是push_back函数通过复制或者move元素来插入。
示例:结果:
myvector contains: 10 20 30 100 200

*/

void myemplace_back()
{
    std::vector<int> myvector = { 10, 20, 30 };
    myvector.emplace_back(100);
    myvector.emplace_back(200);
    std::cout << "myvector contains:";
    for (auto& x : myvector)
        std::cout << ' ' << x;
    std::cout << '\n';

}

/*
分配器的函数
allocator_type get_allocator() const;
函数返回vector相关联的分配器对象的复制。

void myallocated()
{
    std::vector<int> myvector;
    int * p;
    unsigned int i;
    // allocate an array with space for 5 elements using vector's allocator:
    p = myvector.get_allocator().allocate(5);
    // construct values in-place on the array:
    for (i = 0; i<5; i++)
        myvector.get_allocator().construct(&p[i], i);
    std::cout << "The allocated array contains:";
    for (i = 0; i<5; i++)
        std::cout << ' ' << p[i];
    std::cout << '\n';
    // destroy and deallocate:
    for (i = 0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
    myvector.get_allocator().deallocate(p, 5);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值