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。
You made some decent points there. I checked on the net to find out more about the issue and found most individuals will go along with your views on this web site.
This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thanks a lot.
Saved as a favorite, I love your site!
Great post. I will be going through a few of these issues as well..
I used to be able to find good info from your articles.
An impressive share! I’ve just forwarded this onto a friend who has been conducting a little research on this. And he in fact ordered me lunch simply because I discovered it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to discuss this topic here on your website.
Very good post! We will be linking to this particularly great post on our site. Keep up the good writing.
After exploring a handful of the articles on your website, I honestly like your way of blogging. I saved it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and let me know what you think.
I’m amazed, I have to admit. Seldom do I encounter a blog that’s both educative and amusing, and let me tell you, you have hit the nail on the head. The problem is an issue that not enough folks are speaking intelligently about. I’m very happy I came across this in my hunt for something concerning this.
You’ve made some good points there. I looked on the internet for more info about the issue and found most individuals will go along with your views on this web site.
There is definately a lot to know about this subject. I love all the points you’ve made.
Having read this I believed it was really informative. I appreciate you taking the time and energy to put this content together. I once again find myself personally spending a significant amount of time both reading and leaving comments. But so what, it was still worthwhile.
This is a really good tip especially to those new to the blogosphere. Simple but very accurate info… Many thanks for sharing this one. A must read article.
Greetings! Very helpful advice in this particular post! It is the little changes that make the biggest changes. Many thanks for sharing!
This is the perfect web site for anyone who really wants to understand this topic. You understand a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You certainly put a fresh spin on a subject that’s been discussed for a long time. Great stuff, just excellent.
Very good post. I definitely love this website. Keep it up!
I’m impressed, I have to admit. Seldom do I come across a blog that’s both equally educative and amusing, and let me tell you, you’ve hit the nail on the head. The issue is something that not enough people are speaking intelligently about. Now i’m very happy I found this during my hunt for something regarding this.
I was excited to find this web site. I need to to thank you for your time due to this fantastic read!! I definitely liked every little bit of it and i also have you saved as a favorite to look at new information in your website.
This is a topic that is near to my heart… Best wishes! Where can I find the contact details for questions?
I was able to find good information from your blog posts.
Very good article. I’m dealing with a few of these issues as well..
This is a topic that is close to my heart… Thank you! Where are your contact details though?
I blog frequently and I really appreciate your content. Your article has really peaked my interest. I’m going to book mark your site and keep checking for new details about once a week. I subscribed to your RSS feed as well.
Hi, I do think this is an excellent blog. I stumbledupon it 😉 I’m going to come back once again since I book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide other people.
Greetings! Very useful advice in this particular post! It’s the little changes that make the greatest changes. Thanks a lot for sharing!
After looking at a handful of the blog posts on your website, I really like your technique of writing a blog. I book-marked it to my bookmark site list and will be checking back soon. Please check out my web site as well and tell me what you think.
Everything is very open with a clear description of the issues. It was really informative. Your site is very useful. Thanks for sharing!
I must thank you for the efforts you have put in writing this blog. I am hoping to check out the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has encouraged me to get my own website now 😉
I blog quite often and I really thank you for your information. This article has truly peaked my interest. I’m going to take a note of your website and keep checking for new details about once a week. I subscribed to your RSS feed as well.
I wanted to thank you for this good read!! I certainly loved every bit of it. I have you saved as a favorite to look at new stuff you post…
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