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。
Hi! I just want to give you a big thumbs up for your great info you have right here on this post. I am coming back to your web site for more soon.
In October 2020, another $96 million was allocated to more than $5,000 businesses.
Martucci, Joe (June 6, 2020).
Gold chains play a big role in immediately’s life of each modern-day ladies.
Your style is really unique in comparison to other people I’ve read stuff from. Thank you for posting when you’ve got the opportunity, Guess I’ll just book mark this blog.
Way cool! Some extremely valid points! I appreciate you writing this write-up and also the rest of the site is very good.
This site truly has all of the info I wanted concerning this subject and didn’t know who to ask.
Oh my goodness! Awesome article dude! Thanks, However I am encountering problems with your RSS. I don’t know the reason why I am unable to subscribe to it. Is there anybody else getting similar RSS problems? Anyone who knows the answer can you kindly respond? Thanks!
I like it when people get together and share views. Great blog, stick with it.
I’m excited to discover this site. I need to to thank you for ones time due to this fantastic read!! I definitely loved every part of it and i also have you book marked to look at new stuff on your web site.
Next time I read a blog, Hopefully it won’t fail me just as much as this particular one. After all, Yes, it was my choice to read through, but I genuinely believed you would have something useful to talk about. All I hear is a bunch of crying about something you could fix if you weren’t too busy looking for attention.
I like it when folks get together and share ideas. Great site, keep it up!
BJ 88
Greetings! Very helpful advice in this particular article! It’s the little changes that produce the biggest changes. Many thanks for sharing!
I seriously love your blog.. Very nice colors & theme. Did you create this site yourself? Please reply back as I’m trying to create my own personal site and would love to learn where you got this from or exactly what the theme is named. Appreciate it!
Your style is really unique compared to other folks I have read stuff from. Thanks for posting when you have the opportunity, Guess I’ll just book mark this site.
I’m not that much of a online reader to be honest but your blogs really nice, keep
it up! I’ll go ahead and bookmark your site to come back
later on. Cheers
May I simply say what a relief to uncover someone that really knows what they are talking about on the internet. You certainly understand how to bring a problem to light and make it important. More people must read this and understand this side of your story. I was surprised that you aren’t more popular given that you surely have the gift.
Can I simply just say what a comfort to discover someone who actually knows what they’re talking about on the internet. You definitely understand how to bring a problem to light and make it important. A lot more people must look at this and understand this side of your story. I was surprised you’re not more popular because you most certainly possess the gift.
Very good post. I will be dealing with many of these issues as well..
https://789club60.com/
Pretty! This was an extremely wonderful article. Thank you for supplying this info.
This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me. Cheers.
Right here is the right blog for everyone who really 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 new spin on a subject that’s been discussed for years. Excellent stuff, just wonderful.
Your style is very unique compared to other people I’ve read stuff from. Thank you for posting when you’ve got the opportunity, Guess I will just bookmark this site.
I would like to thank you for the efforts you’ve put in writing this site.
I really hope to see the same high-grade blog posts by you in the future as well.
In truth, your creative writing abilities has encouraged me to get my very own blog now 😉
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
I could not resist commenting. Exceptionally well written!
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
You’re so awesome! I do not believe I’ve read through a single thing like that before. So nice to discover somebody with some unique thoughts on this issue. Seriously.. thank you for starting this up. This website is something that is required on the internet, someone with a bit of originality.
After I originally left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I get four emails with the same comment. Perhaps there is a means you can remove me from that service? Many thanks.
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
Good post. I learn something new and challenging on websites I stumbleupon every day. It’s always exciting to read content from other authors and use something from their websites.
I couldn’t resist commenting. Very well written!
I used to be able to find good information from your blog posts.
You need to be a part of a contest for one of the best websites on the internet. I most certainly will recommend this site!
An interesting discussion is definitely worth comment. I do think that you ought to write more about this subject, it may not be a taboo matter but typically people don’t discuss these issues. To the next! Kind regards.
Greetings! Very helpful advice in this particular post! It’s the little changes which will make the most significant changes. Many thanks for sharing!
That is a good tip especially to those new to the blogosphere. Simple but very accurate info… Many thanks for sharing this one. A must read post.
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
You have made some good points there. I looked on the web to learn more about the issue and found most people will go along with your views on this website.
Good info. Lucky me I found your website by chance (stumbleupon). I have bookmarked it for later!
Introducing to you the most prestigious online entertainment address today. Visit now to experience now!
Everything is very open with a very clear clarification of the challenges. It was definitely informative. Your site is very useful. Thanks for sharing.
There’s definately a great deal to learn about this issue. I love all of the points you made.