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。
This is a topic that is close to my heart… Best wishes! Exactly where are your contact details though?
bookmarked!!, I like your web site.
This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thanks.
I’m amazed, I must say. Rarely do I encounter a blog that’s both equally educative and amusing, and without a doubt, you have hit the nail on the head. The issue is an issue that not enough people are speaking intelligently about. I am very happy I found this in my hunt for something relating to this.
Next time I read a blog, I hope that it doesn’t fail me as much as this particular one. After all, I know it was my choice to read, but I genuinely believed you’d have something useful to talk about. All I hear is a bunch of complaining about something you could possibly fix if you were not too busy seeking attention.
Hi there! I could have sworn I’ve visited your blog before but after going through a few of the articles I realized it’s new to me. Regardless, I’m certainly happy I came across it and I’ll be book-marking it and checking back regularly!
Very nice blog post. I certainly appreciate this website. Keep it up!
May I simply just say what a relief to uncover somebody that truly knows what they’re talking about on the internet. You certainly understand how to bring an issue to light and make it important. More and more people ought to read this and understand this side of the story. I was surprised you aren’t more popular since you definitely have the gift.
I was able to find good info from your blog posts.
I have to thank you for the efforts you’ve put in penning this site. I really hope to view the same high-grade blog posts from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own blog now 😉
bookmarked!!, I like your website.
This is a topic that’s near to my heart… Thank you! Where can I find the contact details for questions?
Great site you’ve got here.. It’s hard to find high-quality writing like yours these days. I really appreciate individuals like you! Take care!!
Spot on with this write-up, I really think this amazing site needs far more attention. I’ll probably be back again to read more, thanks for the information!
May I simply say what a relief to find somebody that really knows what they are discussing on the web. You definitely realize how to bring a problem to light and make it important. A lot more people have to check this out and understand this side of your story. I can’t believe you’re not more popular because you surely possess the gift.
Greetings, I do believe your web site could possibly be having internet browser compatibility issues. Whenever I take a look at your site in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping issues. I merely wanted to provide you with a quick heads up! Other than that, wonderful website.
After I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I receive 4 emails with the exact same comment. There has to be an easy method you are able to remove me from that service? Many thanks.
I really like it when folks get together and share ideas. Great blog, stick with it.
Hi there, I think your site could be having web browser compatibility issues. Whenever I look at your web site in Safari, it looks fine however, when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, fantastic blog!
I wanted to thank you for this very good read!! I absolutely loved every bit of it. I have got you book-marked to check out new stuff you post…
Right here is the right web site for everyone who wants to find out about this topic. You realize a whole lot its almost hard to argue with you (not that I personally will need to…HaHa). You definitely put a fresh spin on a subject which has been discussed for a long time. Excellent stuff, just excellent.
Right here is the perfect blog for anyone who wishes to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a new spin on a topic which has been discussed for many years. Great stuff, just great.
Good information. Lucky me I discovered your blog by accident (stumbleupon). I have book-marked it for later!
The next time I read a blog, I hope that it does not fail me as much as this particular one. I mean, Yes, it was my choice to read through, however I truly believed you would probably have something helpful to talk about. All I hear is a bunch of moaning about something that you could possibly fix if you were not too busy seeking attention.
May I just say what a relief to find an individual who really understands what they’re discussing online. You certainly realize how to bring an issue to light and make it important. More people ought to check this out and understand this side of the story. It’s surprising you aren’t more popular because you most certainly have the gift.
bookmarked!!, I like your website.
You have made some decent points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this site.
Great post! We are linking to this particularly great article on our site. Keep up the great writing.
An intriguing discussion is worth comment. I do believe that you ought to publish more on this subject, it might not be a taboo subject but usually people do not discuss these subjects. To the next! Best wishes.
After looking over a number of the articles on your site, I really appreciate your technique of writing a blog. I book marked it to my bookmark webpage list and will be checking back soon. Please check out my web site too and let me know what you think.
Right here is the perfect site for anybody who would like to find out about this topic. You understand a whole lot its almost tough to argue with you (not that I actually will need to…HaHa). You definitely put a new spin on a subject which has been discussed for a long time. Great stuff, just excellent.
Having read this I thought it was very informative. I appreciate you finding the time and energy to put this article together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was still worthwhile!
That is a great tip particularly to those new to the blogosphere. Simple but very accurate info… Thanks for sharing this one. A must read post.
You have made some good points there. I looked on the web for additional information about the issue and found most people will go along with your views on this site.
There’s definately a lot to know about this subject. I like all the points you have made.
You’re so cool! I do not think I’ve truly read through something like that before. So wonderful to discover somebody with original thoughts on this topic. Really.. many thanks for starting this up. This website is one thing that is needed on the web, someone with a little originality.
Way cool! Some very valid points! I appreciate you penning this post and the rest of the site is very good.
Way cool! Some extremely valid points! I appreciate you penning this write-up and the rest of the website is also really good.
There is definately a lot to know about this subject. I love all the points you made.
I needed to thank you for this good read!! I absolutely loved every little bit of it. I have got you saved as a favorite to check out new things you post…
The very next time I read a blog, Hopefully it does not fail me as much as this particular one. After all, Yes, it was my choice to read, nonetheless I actually thought you would probably have something interesting to talk about. All I hear is a bunch of moaning about something you can fix if you were not too busy searching for attention.
Greetings! Very helpful advice within this article! It is the little changes that will make the most significant changes. Thanks a lot for sharing!
That is a really good tip especially to those fresh to the blogosphere. Brief but very accurate information… Appreciate your sharing this one. A must read article.
Very good information. Lucky me I recently found your blog by accident (stumbleupon). I have book-marked it for later.
Very nice blog post. I absolutely appreciate this site. Stick with it!
I was able to find good advice from your blog articles.
bookmarked!!, I really like your site!
I blog quite often and I really appreciate your information. Your article has truly peaked my interest. I will take a note of your blog and keep checking for new information about once per week. I subscribed to your RSS feed as well.
This excellent website certainly has all of the information and facts I needed about this subject and didn’t know who to ask.
I was pretty pleased to uncover this site. I want to to thank you for ones time for this particularly wonderful read!! I definitely liked every bit of it and i also have you bookmarked to see new stuff on your blog.