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。
bookmarked!!, I love your website!
Hello, i believe that i saw you visited my site so i got here to return the favor?.I am attempting to find issues to improve my website!I
assume its ok to use a few of your concepts!!
When 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 recieve 4 emails with the exact same comment.
Perhaps there is a way you are able to remove me from
that service? Many thanks!
Your means of describing the whole thing in this article is in fact good, all be
able to without difficulty know it, Thanks a lot.
Hi there! I could have sworn I’ve been to this site before but after checking through some of the post I realized it’s
new to me. Nonetheless, I’m definitely glad I
found it and I’ll be bookmarking and checking back frequently!
Does your site have a contact page? I’m having a tough time
locating it but, I’d like to shoot you an email.
I’ve got some creative ideas for your blog you might be interested in hearing.
Either way, great site and I look forward to seeing it expand over time.
Hello there! Would you mind if I share your blog with
my twitter group? There’s a lot of folks that I think
would really enjoy your content. Please let me know.
Thanks
http://gjdwvstory.kr/bbs/board.php?bo_table=free&wr_id=212725
I like looking through a post that will make men and women think. Also, thanks for permitting me to comment.
You have made some good points there. I checked on the internet to find out more about the issue and found most people will go along with your views on this web site.
The next time I read a blog, Hopefully it doesn’t fail me as much as this one. After all, Yes, it was my choice to read through, but I truly thought you would probably have something helpful to talk about. All I hear is a bunch of moaning about something that you could fix if you weren’t too busy looking for attention.
https://enrichkor.com/bbs/board.php?bo_table=free&wr_id=29959
Hi! I could have sworn I’ve been to this web site before but after going through some of the posts I realized it’s new to me. Anyhow, I’m definitely happy I came across it and I’ll be book-marking it and checking back regularly!
This website was… how do you say it? Relevant!! Finally I have found something which helped me. Kudos!
Hello! I simply wish to offer you a huge thumbs up for your excellent info you’ve got right here on this post. I am returning to your website for more soon.
After looking at a few of the blog posts on your web site, I honestly like your way of writing a blog. I saved it to my bookmark site list and will be checking back soon. Please check out my website as well and let me know what you think.
This is a topic which is near to my heart… Cheers! Where can I find the contact details for questions?
Having read this I thought it was really informative. I appreciate you taking the time and energy to put this information together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worthwhile.
Your style is very unique in comparison to other folks I’ve read stuff from. Thanks for posting when you’ve got the opportunity, Guess I’ll just bookmark this page.
https://leesunlee.kr/bbs/board.php?bo_table=free&wr_id=1729080
This site definitely has all the info I wanted about this subject and didn’t know who to ask.
Right here is the right blog for everyone who wishes to understand this topic. You understand a whole lot its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a new spin on a topic that has been written about for a long time. Great stuff, just great.
A fascinating discussion is definitely worth comment. I do believe that you ought to publish more on this issue, it may not be a taboo matter but typically people do not talk about these topics. To the next! Cheers.
https://www.jejutongil.com/bbs/bbs/board.php?bo_table=free&wr_id=80663
Everything is very open with a very clear explanation of the issues. It was really informative. Your website is very useful. Many thanks for sharing!
The next time I read a blog, I hope that it doesn’t disappoint me as much as this particular one. I mean, Yes, it was my choice to read, however I truly believed you’d have something helpful to say. All I hear is a bunch of complaining about something that you could fix if you weren’t too busy searching for attention.
This is a topic that’s near to my heart… Best wishes! Where can I find the contact details for questions?
Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the blog.
Any feed-back would be greatly appreciated.
http://www.caresalad.com/bbs/board.php?bo_table=free&wr_id=201769
That is a good tip particularly to those new to the blogosphere. Short but very accurate info… Many thanks for sharing this one. A must read article!
http://sandartworld.com/bbs/board.php?bo_table=free&wr_id=17045
https://www.outlookindia.com/plugin-play/ED8590ED8590EBB2B3-EB8F99EC9DBC-EC8694EBA3A8EC8598-EBA994EC9DB4ECA080EC82ACEC9DB4ED8AB8-EBAAA8EC9584EBB3B4EAB8B0
Spot on with this write-up, I absolutely believe this amazing site needs a great deal more attention. I’ll probably be returning to read through more, thanks for the info!
Fantastic beat ! I wish to apprentice while you amend your website,
how could i subscribe for a blog website? The account
helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept
I really like reading through a post that will make people think. Also, thank you for allowing me to comment.
https://solo.to/kobet1
In my experience, properly calibrated current transducers can greatly enhance overall system efficiency.
When working with current sensors, how crucial do you
find calibration to achieving reliable readings?
Also visit my blog … accurate current detection systems
I love it when individuals get together and share ideas. Great blog, continue the good work.
You ought to be a part of a contest for one of the highest quality blogs on the web. I will highly recommend this site!
I like what you guys are up too. This sort of clever work and coverage!
Keep up the terrific works guys I’ve included you guys to my personal blogroll.
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you!
However, how can we communicate?
I was curious if you ever thought of changing the page layout of your website?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having one or
2 pictures. Maybe you could space it out better?
Pretty section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed account
your blog posts. Any way I will be subscribing to your augment and even I achievement you access
consistently rapidly.
I seriously love your website.. Pleasant colors & theme. Did you make this website yourself? Please reply back as I’m wanting to create my own personal site and want to find out where you got this from or just what the theme is called. Thanks!
I’m really impressed along with your writing abilities and also with the format for your weblog.
Is that this a paid topic or did you modify
it your self? Either way keep up the excellent quality writing, it
is uncommon to see a nice weblog like this one these days..
Great weblog here! Also your site lots up fast! What web host are you the use of?
Can I get your affiliate hyperlink on your host?
I want my website loaded up as quickly as yours lol
I used to be able to find good information from your content.
Very good post. I’m going through a few of these issues as well..
I really like reading through an article that will make men and women think. Also, many thanks for permitting me to comment.
Good post. I learn something new and challenging on sites I stumbleupon everyday. It’s always exciting to read articles from other authors and use a little something from their web sites.