码迷,mamicode.com
首页 > 其他好文 > 详细

STL vector中的front方法(4)

时间:2014-08-09 18:45:48      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:stl   front   

public member function
<vector>

std::vector::front

      reference front();
const_reference front() const;
Access first element

访问第一个元素

Returns a reference to the first element in the vector.

返回第一个元素的引用。


Unlike member vector::begin, which returns an iterator to this same element, this function returns a direct reference.

和begin不一样,begin是返回一个迭代器,而front是返回一个直接引用。



Calling this function on an empty container causes undefined behavior.

对一个空的容器调用该方法将会导致不可预料的结果


Parameters

none

Return value

A reference to the first element in the vector container.

一个指向该vector容器中第一个元素的引用.


If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

如果该vector是const属性的,那么返回值也将是const属性的,否则,就是返回一个普通的iterator。


Member types reference and const_reference are the reference types to the elements of the container (see vector member types).

返回值的类型都是模版元素定义的类型。即元素类型。


Example

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

int main ()
{
  std::vector<int> myvector;

  myvector.push_back(78);
  myvector.push_back(16);

  // now front equals 78, and back 16

  myvector.front() -= myvector.back();

  std::cout << "myvector.front() is now " << myvector.front() << ‘\n‘;

  return 0;
}


Output:
myvector.front() is now 62

Complexity

Constant.

Iterator validity

No changes.
调用该方法不会使其他迭代器失效。

Data races

The container is accessed (neither the const nor the non-const versions modify the container).

容器可以被访问(该操作不会修改容器内容)

The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

该引用可以用来访问或者是修改元素,并且他们都是安全的。


Exception safety

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

如果容器非空,该方法不会抛出异常。


Otherwise, it causes undefined behavior.

否则,会导致未知的行为!


//翻译的不好的地方麻烦大家指出来以便修改,谢谢。

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

STL vector中的front方法(4),布布扣,bubuko.com

STL vector中的front方法(4)

标签:stl   front   

原文地址:http://blog.csdn.net/qq844352155/article/details/38458047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!