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’re so cool! I do not believe I have read through something like this before. So wonderful to find another person with a few genuine thoughts on this issue. Seriously.. thank you for starting this up. This site is one thing that is required on the internet, someone with a little originality.
Greetings! Very useful advice in this particular article! It’s the little changes which will make the greatest changes. Thanks for sharing!
https://anotepad.com/notes/dkyb49pi
Hi there! I could have sworn I’ve been to this blog before but after looking at some of the posts I realized it’s new to me. Anyhow, I’m certainly delighted I discovered it and I’ll be book-marking it and checking back often.
Greetings! Very helpful advice within this post! It is the little changes that will make the greatest changes. Thanks for sharing!
Can I just say what a relief to find a person that truly understands what they are discussing on the net. You certainly understand how to bring an issue to light and make it important. More and more people really need to look at this and understand this side of your story. It’s surprising you’re not more popular given that you most certainly possess the gift.
This is the perfect blog for anyone who would like to understand this topic. You realize a whole lot its almost tough to argue with you (not that I personally would want to…HaHa). You definitely put a fresh spin on a topic that has been written about for many years. Great stuff, just wonderful.
Spot on with this write-up, I actually believe that this amazing site needs much more attention. I’ll probably be returning to read more, thanks for the information!
An interesting discussion is worth comment. I believe that you ought to write more about this subject matter, it might not be a taboo subject but typically folks don’t speak about such issues. To the next! Many thanks!
I’m impressed, I must say. Seldom do I come across a blog that’s equally educative and interesting, and without a doubt, you’ve hit the nail on the head. The problem is something that not enough folks are speaking intelligently about. Now i’m very happy that I stumbled across this during my hunt for something concerning this.
After I initially commented 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 4 emails with the exact same comment. Is there a means you can remove me from that service? Cheers.
I’m amazed, I must say. Rarely do I encounter a blog that’s equally educative and entertaining, and without a doubt, you’ve hit the nail on the head. The problem is an issue that too few people are speaking intelligently about. Now i’m very happy I came across this during my hunt for something regarding this.
That is a good tip especially to those new to the blogosphere. Short but very accurate info… Thank you for sharing this one. A must read article!
Hi! I could have sworn I’ve been to your blog before but after browsing through some of the posts I realized it’s new to me. Regardless, I’m certainly happy I stumbled upon it and I’ll be bookmarking it and checking back often!
I pay a quick visit day-to-day some sites and sites to read content, except this
web site gives feature based posts.
It’s hard to come by experienced people about this topic, however, you seem like you know what you’re talking about! Thanks
Thanks to my father who told me regarding this web site, this
website is truly remarkable.
At this time it seems like WordPress is the top blogging platform out there right
now. (from what I’ve read) Is that what you are using on your blog?
My brother recommended I would possibly like this web site.
He was totally right. This submit truly made my day. You can not consider simply
how so much time I had spent for this information! Thank you!
Hi! I could have sworn I’ve visited this site before but after going
through a few of the posts I realized it’s new to me.
Anyhow, I’m certainly delighted I discovered it and I’ll be
bookmarking it and checking back regularly!
Hi there outstanding blog! Does running a blog such as this take
a great deal of work? I have virtually no expertise in computer programming but I had been hoping to start my own blog soon.
Anyways, if you have any recommendations or techniques for new blog owners please
share. I understand this is off topic however I simply wanted
to ask. Thank you!
I’m impressed, I must say. Seldom do I come across a blog that’s equally educative and interesting,
and without a doubt, you’ve hit the nail on the head.
The problem is something which too few men and women are speaking intelligently about.
I am very happy I came across this in my hunt for something
concerning this.
Awesome post.
I like what you guys are usually up too. Such clever work and reporting!
Keep up the fantastic works guys I’ve added you guys to blogroll.
For the reason that the admin of this web page is
working, no question very soon it will be famous, due to its feature contents.
What’s up, I wish for to subscribe for this website to get most recent updates, so where can i do it please help out.
Spot on with this write-up, I truly think this site needs far more attention. I’ll probably be returning to read more, thanks for the advice!
Greetings from Idaho! I’m bored at work so I decided
to check out your website on my iphone during lunch
break. I enjoy the information you present here and
can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyhow, awesome blog!
Quality articles or reviews is the crucial to attract the visitors to pay a quick
visit the website, that’s what this website is providing.
Having read this I thought it was rather enlightening. I appreciate you spending some time and effort to put this informative article together. I once again find myself spending a lot of time both reading and leaving comments. But so what, it was still worthwhile!
http://story119.com/bbs/board.php?bo_table=free&wr_id=284632
This is very interesting, You’re a very skilled blogger.
I’ve joined your feed and look forward to seeking more of your magnificent post.
Also, I’ve shared your web site in my social networks!
Incredible points. Solid arguments. Keep up the great spirit.
You should be a part of a contest for one of the greatest blogs on the internet. I am going to recommend this website!
An interesting discussion is definitely worth comment. I believe that you ought to publish more about this topic, it might not be a taboo matter but typically people don’t discuss these subjects. To the next! Best wishes.
Hello, There’s no doubt that your blog could possibly be having web browser compatibility issues. Whenever I look at your blog in Safari, it looks fine however, when opening in IE, it’s got some overlapping issues. I just wanted to give you a quick heads up! Apart from that, excellent site!
Right here is the perfect website for anybody who hopes to find out about this topic. You know so much its almost hard to argue with you (not that I really will need to…HaHa). You certainly put a brand new spin on a topic which has been written about for ages. Excellent stuff, just great.
This post is priceless. Where can I find out more?
An interesting discussion is worth comment. I do think that you should publish more about this topic,
it may not be a taboo subject but generally people do not
speak about these subjects. To the next! Many thanks!!
Having read this I believed it was rather enlightening. I appreciate you taking the time and effort to put this informative article together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worthwhile!
Great beat ! I would like to apprentice while you
amend your site, how can i subscribe for a blog site?
The account helped me a acceptable deal. I had been tiny bit acquainted
of this your broadcast offered bright clear idea
Undeniably consider that that you stated. Your favourite justification appeared to be
at the internet the easiest thing to have in mind of.
I say to you, I definitely get irked while other people consider worries that they
just don’t recognize about. You controlled to hit the nail upon the highest as smartly as outlined out
the entire thing with no need side effect , folks can take
a signal. Will likely be again to get more. Thank you
Thanks for sharing your thoughts on c/c++. Regards
Thanks designed for sharing such a fastidious thought, paragraph is pleasant, thats why i have read it entirely
Hello there! This post couldn’t be written much better! Looking through this article reminds me of my previous roommate! He continually kept preaching about this. I most certainly will send this post to him. Pretty sure he’ll have a great read. I appreciate you for sharing!
It’s an amazing paragraph in support of all the internet users; they will take advantage from it I am sure.
I was able to find good information from your blog articles.
Thanks for sharing your thoughts on c/c++. Regards
I’m amazed, I must say. Seldom do I encounter 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
which too few folks are speaking intelligently about.
I am very happy that I came across this during my search for something concerning
this.
Hello there! This post couldn’t be written any better!
Going through this article reminds me of my previous roommate!
He continually kept talking about this. I’ll forward this information to him.
Fairly certain he’s going to have a very good read. Thank you
for sharing!