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

/ 114评论 / 1413阅读 / 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. 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

  2. paccar davie4说道:

    I love it when individuals get together and share ideas. Great blog, continue the good work.

  3. 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!

  4. ufo5555说道:

    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.

  5. naga169 slot说道:

    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?

  6. TEKTK77说道:

    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?

  7. TEKTK77说道:

    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.

  8. Escorts说道:

    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!

  9. naga169说道:

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

  10. Tawna说道:

    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

  11. I used to be able to find good information from your content.

  12. Very good post. I’m going through a few of these issues as well..

  13. bobres-iptv说道:

    I really like reading through an article that will make men and women think. Also, many thanks for permitting me to comment.

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

发表回复

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