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;//会报错
- -
- 运行结果:
- -
- 在末尾的两行,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。
sky88
88 clb
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
An intriguing discussion is worth comment. I do think that you should write more on this topic, it might not be a taboo subject but generally folks don’t talk about these subjects. To the next! Kind regards!
A motivating discussion is definitely worth comment. I do think that you need to write more about this subject, it might not be a taboo matter but usually people do not speak about such topics. To the next! Cheers!
Thank you so much!
I enjoy looking through a post that can make people think. Also, thanks for allowing for me to comment.
You need to take part in a contest for one of the best sites on the internet. I’m going to highly recommend this web site!
I used to be able to find good info from your content.
Everyone loves it when people come together and share views. Great site, keep it up.
Hi there! This post could not be written any better! Going through this article reminds me of my previous roommate! He always kept talking about this. I’ll forward this article to him. Pretty sure he’s going to have a good read. I appreciate you for sharing!
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
Oh my goodness! Awesome article dude! Many thanks, However I am encountering problems with your RSS. I don’t understand the reason why I cannot subscribe to it. Is there anybody else having the same RSS problems? Anyone who knows the solution will you kindly respond? Thanx.
I was very happy to find this web site. I need to to thank you for your time for this fantastic read!! I definitely liked every little bit of it and I have you book marked to check out new stuff in your blog.
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
An impressive share! I have just forwarded this onto a friend who was conducting a little homework on this. And he in fact bought me lunch due to the fact that I found it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending some time to discuss this matter here on your site.
This site really has all the information I needed about this subject and didn’t know who to ask.
Hello, I read your blogs daily. Your writing style is awesome, keep it up!
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
This is the right webpage for anybody who would like to understand this topic. You know so much its almost hard to argue with you (not that I actually would want to…HaHa). You definitely put a fresh spin on a topic which has been written about for decades. Wonderful stuff, just great.
Hello, There’s no doubt that your blog could be having browser compatibility problems. Whenever I look at your web site in Safari, it looks fine but when opening in IE, it has some overlapping issues. I merely wanted to provide you with a quick heads up! Apart from that, excellent blog!
Very good write-up. I definitely love this website. Stick with it!
Fantastic goods from you, man. I have understand your stuff previous to and you’re just too
magnificent. I actually like what you have acquired here, really like what you are stating and the
way in which you say it. You make it enjoyable and you still care for to keep it wise.
I cant wait to read much more from you. This is really a great site.
May I simply just say what a comfort to find someone that genuinely understands what they’re talking about online. You certainly understand how to bring a problem to light and make it important. More people must read this and understand this side of the story. I was surprised that you’re not more popular since you certainly possess the gift.
It’s hard to come by well-informed people for this topic, but you seem like you know what you’re talking about! Thanks
The next time I read a blog, Hopefully it does not disappoint me just as much as this one. After all, Yes, it was my choice to read through, nonetheless I actually thought you would have something helpful to say. All I hear is a bunch of crying about something you could possibly fix if you weren’t too busy seeking attention.
Greetings! Very useful advice within this post! It is the little changes which will make the greatest changes. Thanks for sharing!
Greetings! Very useful advice in this particular post! It is the little changes which will make the most significant changes. Thanks a lot for sharing!
Saved as a favorite, I like your web site.
Hello there! This blog post could not be written any better! Looking through this post reminds me of my previous roommate! He always kept preaching about this. I will forward this post to him. Pretty sure he’s going to have a very good read. I appreciate you for sharing!
Good web site you have got here.. It’s difficult to find quality writing like yours nowadays. I really appreciate people like you! Take care!!
sugar defender ingredients For several
years, I’ve battled unforeseeable blood sugar swings that left me
feeling drained pipes and lethargic. But because incorporating Sugar Protector right into my regular, I’ve observed a
substantial renovation in my overall power and security.
The dreaded mid-day thing of the past, and I value that
this all-natural solution attains these results with no unpleasant or negative reactions.
truthfully been a transformative exploration for me.
Howdy! This post could not be written any better! Going through this post reminds me of my previous roommate! He continually kept preaching about this. I most certainly will forward this post to him. Fairly certain he’s going to have a great read. I appreciate you for sharing!
Quay lén trong phòng wc
Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be interesting to read through articles from other authors and use something from other websites.
Can I simply just say what a relief to uncover someone who actually understands what they are discussing on the net. You certainly know how to bring an issue to light and make it important. A lot more people have to look at this and understand this side of the story. I was surprised you are not more popular because you certainly have the gift.
Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the website is also very good.
There’s certainly a lot to learn about this topic. I really like all of the points you’ve made.
Greetings! Very useful advice within this post! It’s the little changes which will make the biggest changes. Many thanks for sharing!
You ought to be a part of a contest for one of the highest quality blogs online. I will highly recommend this website!