stl 向量_C ++ STL第1部分:向量

stl 向量

1.什么是C ++ STL? (1. What is C++ STL?)

STL代表标准模板库,并且是标准C ++库的一部分。 它包含许多有用的数据结构(容器)和算法,可以节省大量时间。 今天,我们将看看STL Vector。

2. C ++经典固定大小数组 (2. C++ classic fixed size arrays)

曾经用C ++编程的每个人都遇到过经典的固定大小数组。 这是一个例子。
//declare array (indexes are from 0 to 999 (1000 elements), be careful)
int arr[1000];

int num;

cout << "How many numbers will you write?" << endl;
cin >> num;

//read NUM integers and write them into array
for(int i=0; i<num; ++i) {
	cin >> arr[i];
}

num elements from the user and stored them into array. If we wanted to read more than 1000 values, we would overflow the array, resulting in undefined behaviour. The place for 1000 integers is defined when program is compiled and cannot be changed at runtime. This can be overcame by dynamic memory allocation. (See below)

用户的num个元素,并将它们存储到数组中。 如果我们想读取1000个以上的值,则会使数组溢出,从而导致未定义的行为。 1000个整数的位置是在编译程序时定义的,不能在运行时更改。 这可以通过动态内存分配来克服。 (见下文)

3. C ++动态内存分配数组 (3. C++ dynamic memory allocation arrays)

看一下以下代码片段:
int num;

cout << "How many numbers will you write?" << endl;
cin >> num;

int *arr=new int[num];
for (int i=0; i<num; ++i) {
	cin >> arr[i];
}

delete arr[];

The more modern way of doing this is a vector, which can change its size during the program execution. We will take a look at it in the next chapter.

一个更现代的实现方法是向量,它可以在程序执行期间更改其大小。 我们将在下一章中对其进行研究。

4. C ++ STL矢量 (4. C++ STL Vector)

向量与数组的数据结构非常相似,不同之处在于向量将在即将溢出时重新分配自身。 它们的使用方式也非常相似。 但是首先,要使用C ++ Vector,我们需要包括STL矢量库。 它是如此简单:
#include <vector>
vector<int> first; //creates empty vector of integers
vector<int> second(100, 0); //creates vector with 100 zeroes
vector<float> third(3, 5.3); //creates vector, with three values 5.3
vector<int> fourth(second); //creates a copy of second vector

5.向Vector添加(附加)元素 (5. Adding (appending) elements to Vector)

要将一个元素添加到向量的后面,可以使用

vector.push_back(value). See the example below. It "pushes back" elements to the back of the vector until user inputs 0 (zero).

vector.push_back(value) 。 请参见下面的示例。 它将元素“推回”向量的背面,直到用户输入0(零)为止。

vector<int> first; //creates empty vector of integers

//read until user writes 0
int read;
while(true) {
	cin >> read;
	if (read==0) break;
	first.push_back(read);
};

6.计算元素数量 (6. Counting the number of elements)

这是一件简单的事情,并且比经典数组有很多好处,在经典数组中,您必须跟踪特殊变量(整数)中的元素数量。 这就像调用一样简单

vector.size(). See the example. It displays the count of numbers in the previous example.

vector.size()。 参见示例。 它显示上一个示例中的数字计数。

cout << "You have written " << first.size() << " numbers.";

7.访问/更改元素 (7. Accessing / changing the elements)

有很多方法可以做到这一点。 让这些例子说明一切。 该代码是很好的注释。
cout << "Last number was: " << first.back();
first.back()=10; //change the last number to 10

cout << "First number was: " << first.front();
first.front()=10; //change the first number to 10

cout << "Third element was: " << first[2];
cout << "Third element was: " << first.at(2);
first[2]=10; //Change third number to 10
first.at(2)=10; //Change third number to 10

If there are less elements in the vector than n, then .at(n) will throw an exception (aka. report an error), while [n] will produce undefined result.

如果向量中的元素少于n,则.at(n)将引发异常(也称为报告错误),而[n]将产生未定义的结果。

8.清除向量 (8. Clearing the vector)

在某些情况下,您需要清空(擦除所有向量)整个向量。 这很简单。
first.clear();

9.删除最后一个元素 (9. Removing the last element)

简单。 和你打电话一样

vector.push_back(value), call

vector.push_back(value),调用

vector.pop_back().

vector.pop_back()。

first.pop_back();

10.为什么要使用STL Vector? (10. Why to use STL Vector?)

使用STL向量,许多事情变得更加简单。 用它!

11.执行速度如何? (11. What about the speed of execution?)

如果您打开编译器的2级或3级优化,基准测试将向我们表明没有速度障碍或最低速度的缺点。 太好了,您没有理由避免使用向量!
g++ code.cpp -O3

12.想了解更多吗? (12. Want to know more?)

如果我引起您的注意,并且您想进一步了解STL Vector,请查看C ++参考页:http://www.cplusplus.com/reference/stl/vector/vector/

To be countinued... (STL part 2: Strings)

值得计数...(STL第2部分:字符串)

翻译自: https://www.experts-exchange.com/articles/2800/C-STL-part-1-Vectors.html

stl 向量

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值