vector
* vector是c++的一个模板容器,同时也是一个动态数组,是数组就意味着它支持随机访问。
* 它几乎与arraylist一样,但vector是线程安全的,因此vector的性能比arraylist弱。
* 使用时需要#include <vector>
注意
- 指向vector里面的元素的指针是迭代器iterator,而不是一般的变量的那个星号*。
- back():返回最后一个元素的引用。
- end(): 返回的迭代器不可用,它返回的是指向最后一个元素的下一位置,因此想要访问最后一个元素,应该写end()-1,或使用back()。
- 当vector内没有元素时,begin()和end()的返回值一样。
- 注意对迭代器的运算顺序
- 末尾两行如果执行会在运行时报错:
- cannot seek vector iterator before begin
- 即迭代器移动越界了
- 末尾两行如果执行会在运行时报错:
vector<int> arr;
for (int i = 10; i--;) //向vector压入数据
arr.push_back(i);
vector<int>::iterator it_p = arr.begin();//获取vector第一个的迭代器
for (; it_p < arr.end(); ++it_p)//依次顺序输出
cout << *it_p << " ";
cout << arr.back() << endl; //输出最后一个元素的值
it_p = arr.begin();
it_p += 4; //把迭代器移动4个位置
cout << *(it_p - 3) << endl; //不会报错
//cout << *(it_p - 6 + 3) << endl;//如果编译器有优化提前计算出了结果-3,则不会报错
int a = 6, b = 3;
//cout << *(it_p - a + b) << endl;//会报错
- -
- 运行结果:
![](https://blog.coolight.cool/wp-content/uploads/2022/03/image-12.png)
- -
- 在末尾的两行,it_p本来是指向第5个位置(下标为4),此时(it_p - 6 + 3)先算 it_p - 6 则会因为越界而在运行时报错。尽管它最终的结果等同于it_p - 3 是不会越界的。最后一句也是如此。
- 解决方法当然也很简单,把后面的数值计算加个括号,注意符号可能需要改变
- 如 (it_p - 6 + 3)改为(it_p - (6 - 3))或(it_p + (3 - 6))
- 同理(it_p - a + b)改为 (it_p - (a - b)) 或 (it_p + (b - a))
- 当然vector是动态数组,是可以用[]随机访问的,即支持arr[-6 + 7],这样写是没有问题的,它会先计算最终结果(-6 + 7)= 1。
May I simply say what a comfort to uncover somebody that genuinely knows what they are talking about on the net. You definitely realize how to bring an issue to light and make it important. More and more people should look at this and understand this side of the story. I was surprised that you are not more popular given that you certainly have the gift.
Pretty! This has been a really wonderful article. Many thanks for providing this info.
Very good post! We will be linking to this particularly great post on our site. Keep up the good writing.
Greetings! Very useful advice in this particular post! It’s the little changes that will make the biggest changes. Thanks for sharing!
Hello, There’s no doubt that your website could possibly be having internet browser compatibility issues. When I take a look at your blog in Safari, it looks fine but when opening in IE, it’s got some overlapping issues. I merely wanted to give you a quick heads up! Other than that, excellent website!
Oh my goodness! Awesome article dude! Thanks, However I am encountering difficulties with your RSS. I don’t know the reason why I can’t join it. Is there anybody else getting the same RSS problems? Anybody who knows the solution will you kindly respond? Thanks.
I’m very happy to uncover this great site. I need to to thank you for your time for this fantastic read!! I definitely enjoyed every part of it and i also have you saved to fav to look at new things in your site.
The very next time I read a blog, I hope that it does not disappoint me just as much as this particular one. I mean, Yes, it was my choice to read, but I genuinely thought you’d have something helpful to talk about. All I hear is a bunch of moaning about something you can fix if you were not too busy looking for attention.
Hello there, I do believe your blog could possibly be having web browser compatibility issues. Whenever I take a look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I just wanted to give you a quick heads up! Apart from that, wonderful website.
I could not resist commenting. Exceptionally well written.
You’re so interesting! I do not think I’ve read anything like that before. So great to find another person with original thoughts on this subject. Really.. thank you for starting this up. This website is one thing that’s needed on the internet, someone with a bit of originality.
An interesting discussion is definitely worth comment. I believe that you need to write more about this subject, it may not be a taboo subject but typically people don’t speak about such topics. To the next! Best wishes.
You made some really good points there. I looked on the internet to learn more about the issue and found most individuals will go along with your views on this web site.
Everything is very open with a precise explanation of the challenges. It was definitely informative. Your website is very helpful. Thanks for sharing!
I like this weblog very much, Its a very nice situation to read and receive information..
Great article and straight to the point. I am not sure
if this is actually the best place to ask but do you guys have
any thoughts on where to hire some professional writers?
Thx 🙂 Lista escape roomów
Localize por meio do software de sistema “Find My Mobile” que acompanha o telefone ou por meio de software de localização de número de celular de terceiros.
buy cialis online usa The system will be available Friday, October 30th
Velazquez Zamora et al buying cialis online
buy cialis daily online Inflammation can lead to increased pressure in the medullary bone, which can cause the infection to break through the cortex and periosteum