`
阿尔萨斯
  • 浏览: 4270440 次
社区版块
存档分类
最新评论

STL vector中的pop_back方法(22)

 
阅读更多
public member function
<vector>

std::vector::pop_back

void pop_back();
Delete last element
Removes the last element in thevector, effectively reducing the containersizeby one.
可以高效地移除vector中的最后一个元素.

This destroys the removed element.

将销毁并移除该元素。

例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"vi is :"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	vi.pop_back();
	cout<<endl<<"after the pop_back():"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	cout<<endl<<"try to access vi[2]="<<vi[2]<<endl;
	



}


结果截图:


果然又是逗我呢,还是没有销毁数据阿。



Parameters

none

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// vector::pop_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int sum (0);
  myvector.push_back (100);
  myvector.push_back (200);
  myvector.push_back (300);

  while (!myvector.empty())
  {
    sum+=myvector.back();
    myvector.pop_back();
  }

  std::cout << "The elements of myvector add up to " << sum << '\n';

  return 0;
}

In this example, the elements are popped out of thevectorafter they are added up in the sum. Output:
The elements of myvector add up to 600

Complexity

Constant.

Iterator validity

Theend iteratorand any iterator, pointer and reference referring to the removed element are invalidated.

通过end()获得的iterator以及其他指向被移除的元素的迭代器,指针,引用都将失效。

例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"vi is :"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	auto it=vi.end();
	int &ri=vi.back();
	int *p=&vi[2];
	cout<<endl<<"vi.end()="<<*it<<endl;
	cout<<"ri="<<ri<<endl;
	cout<<"*p="<<*p<<endl;
	vi.pop_back();
	cout<<endl<<"after the pop_back():"<<endl;
	for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
	cout<<endl<<"try to access vi[2]="<<vi[2]<<endl<<endl;
	cout<<"vi.end()="<<*it<<endl;
	cout<<"ri="<<ri<<endl;
	cout<<"*p="<<*p<<endl;
	



}


其他的迭代器,指针以及引用,只要不是指向被移除的那个元素,都有效。


Data races

The container is modified.
The last element is modified. Concurrently accessing or modifying other elements is safe, although iterating ranges that include the removed element is not.

容器将被修改。

最后一个元素将被修改。访问以及修改其他元素都是安全的,但是不包括范围在被移除那个元素的例外。

Exception safety

If the container is notempty, the function never throws exceptions (no-throw guarantee).

Otherwise, it causesundefined behavior.

如果容器非空,不会抛出异常,否则,将会导致未知的错误。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi;
	cout<<"vi.pop_back()"<<endl;
	vi.pop_back();
	



}
在我这里似乎没有什么反应啊,难道真的是编译器问题?




//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-16

于GDUT



分享到:
评论

相关推荐

    vector链表实现,STL

    如果将节点变为数组,将会常熟优化。vector链表实现。...拥有iterator,begin,end,rbegin,rend,operator[],push_back,pop_back,push_front,pop_front,size等海量函数以及STL函数支持,也支持RE判断!

    vector常用方法

    Vector 是 C++ STL 中的一种容器,用于存储对象数组。以下是 Vector 的常用方法: 1. push_back():在数组的最后添加一个数据。 2. pop_back():去掉数组的最后一个数据。 3. at():得到编号位置的数据。 4. begin...

    C++之STL的vector详解,包括初始化和各种函数:vector的初始化、数据的增删查改等

    vector的详解那篇 一、vector的初始化 ...7、尾部删去 pop_back() 8、删区间 9、删指定位置 10、清空 四、其他接口 1、size()成员函数 2、empty() 3、resize(int num) 4、capacity() 5、reserve(int len) 总结

    清华大学C++课件中vector用法实例

    清华大学C++课件中vector用法实例 本文将详细介绍清华大学C++课件中vector用法实例,涵盖了vector的基本操作、算法和函数对象的使用。...通过本文,读者可以更好地理解C++ STL中的vector容器,并掌握它的使用方法。

    c++STL基本容器用法带程序详解

    vector用于存储对象数组 常用方法 1.push_back 在数组的最后添加一个数据 2.pop_back 去掉数组的最后一个数据 3.at 得到编号位置的数据 4.begin 得到数组头的指针 5.end 得到数组的最后一个单元+1的指针 6.front ...

    c++中vector的用法详解.doc.docx

    它是 STL(Standard Template Library)的组成部分,提供了多种操作和管理数据的方法。本文将详细介绍 C++ 中 vector 的用法、函数调用和内存管理机制。 vector 的用法 1. 文件包含:在程序开头处加上 `#include ...

    STL源码剖析.pdg

    4.2.6 vector 的元素操作:pop_back, erase, clear, insert 123 4.3 list 128 4.3.1 list 概述 128 4.3.2 list 的节点(node) 129 4.3.3 list 的迭代器 129 4.3.4 list 的数据结构 131 4.3.5 list 的构造与...

    STL容器的一些使用简介

    `vector`是一个动态数组容器,提供了`push_back`、`pop_back`、`at`等操作: ```cpp std::vector&lt;int&gt; v; v.push_back(1); v.push_back(2); std::cout (0) ; // 输出为 1 ``` 然后,让我们来谈谈`string`容器。`...

    STL 源码剖析(侯捷先生译著)

    4.2.6 vector 的元素操作:pop_back, erase, clear, insert 123 4.3 list 128 4.3.1 list 概述 128 4.3.2 list 的节点(node) 129 4.3.3 list 的迭代器 129 4.3.4 list 的数据结构 131 4.3.5 list 的构造与...

    c++ vector(向量)使用方法详解.pdf

    C++中的`std::vector`是一个动态数组,属于标准模板库(STL)的一部分,提供了一种高效的方式来存储和操作元素序列。在C++编程中,`vector`常被用来代替传统的C风格数组,因为它提供了更多的便利功能和安全性。 ###...

    c++ STL思维导图(自己总结)

    本文将总结C++ STL中的主要容器和算法,包括vector、deque、list、set、map、queue、stack和string等。 Vector容器 Vector容器是C++ STL中最常用的容器之一,用于存储同类型的元素。Vector容器提供了多种构造函数...

    c++ vector(向量)使用方法详解.docx

    `vector`是C++标准模板库(STL)的一部分,提供了高效且便捷的方式来存储和操作元素序列。以下是关于`vector`的详细说明: ### 初始化 `vector`的初始化可以通过多种方式实现: 1. `vector&lt;int&gt; a(10);` 创建一个包含...

    vector 使用方法1

    在C++中,`std::vector`是一种非常重要的容器,它是C++标准模板库(STL)的一部分。`vector`可以存储任何类型的元素,通过模板参数来定义元素类型。它像一个动态数组,允许在运行时调整大小。下面将详细介绍`vector`...

    C++ vector的讲解

    * `v.pop_back()` 删除向量中最后一个元素 * `v.clear()` 清空向量中所有元素 * `v.erase(pos)` 删除向量中迭代器指向元素 * `v.erase(first,last)` 删除向量中[first,last)中元素 遍历元素 遍历 Vector 中元素...

    超快复习C++_STL篇4

    在 STL 中,容器是用来存储和管理数据的,迭代器是用来访问容器中元素的接口,算法是用来对容器中元素进行操作的。 .STL 组成: * 迭代器(Iterator):是一种接口,提供了访问容器中元素的方式。 * 容器...

    动态数组vector用法.docx

    * `pop_back()`: 删除vector中的最后一个元素。 * `push_back(elem)`: 在vector的末尾添加一个elem拷贝。 * `rbegin()`和`rend()`: 返回vector的逆向迭代器。 访问vector 有两种方式可以访问vector中的元素: *...

    ACM中的常用的STL容器及其操作说明汇总

    ACM 中常用的 STL 容器及其操作说明汇总 STL(Standard Template Library)是 C++ 编程语言中的一种标准模板库,提供了一些常用的数据结构和算法,帮助程序员快速地开发应用程序。以下是 ACM 竞赛中常用的 STL 容器...

    C++ STL Adaptor stack、queue和vector的使用.doc

    C++ STL Adaptor stack、queue和vector的使用 C++ STL 提供了多种容器类,包括 stack、...以上就是 C++ STL 中 stack、queue 和 priority queue 的使用方法和常见操作。这些容器类可以方便地实现各种数据结构和算法。

    部分NOIP常用STL用法整理

    今天,我们将详细介绍 STL 中的 vector、stack 和 set 等容器类的使用方法。 vector vector 是 STL 中的一种内存连续、长度可变的动态数组。虽然 vector 是动态数组,但其底层仍是定长数组。当数组大小不足时,...

Global site tag (gtag.js) - Google Analytics