[c++]记录使用vector遇到过的天坑问题

/ 20评论 / 1023阅读 / 0点赞

vector

* vector是c++的一个模板容器,同时也是一个动态数组,是数组就意味着它支持随机访问。

* 它几乎与arraylist一样,但vector是线程安全的,因此vector的性能比arraylist弱。

* 使用时需要#include <vector>

注意

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;//会报错

  1. 789bet说道:

    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.

  2. Pretty! This has been a really wonderful article. Many thanks for providing this info.

  3. 78win说道:

    Very good post! We will be linking to this particularly great post on our site. Keep up the good writing.

  4. Leroy Chung说道:

    Greetings! Very useful advice in this particular post! It’s the little changes that will make the biggest changes. Thanks for sharing!

  5. kids cloths说道:

    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!

  6. 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.

  7. iptv说道:

    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.

  8. iptv说道:

    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.

  9. iptv说道:

    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.

  10. kontol besar说道:

    I could not resist commenting. Exceptionally well written.

  11. Mushroom gummies说道:

    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.

  12. asbestos说道:

    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.

  13. sex说道:

    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.

  14. Everything is very open with a precise explanation of the challenges. It was definitely informative. Your website is very helpful. Thanks for sharing!

  15. Shirley H说道:

    I like this weblog very much, Its a very nice situation to read and receive information..

  16. 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

  17. Rastrear Celular说道:

    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.

  18. YpKjqgSW说道:

    buy cialis online usa The system will be available Friday, October 30th

  19. Authelm说道:

    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

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注