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