[关闭]
@StarSky 2017-06-18T08:27:05.000000Z 字数 1272 阅读 647

2017-06-06 Vector clear/extend/iterator, Struct for return

工作日记 C++


initial vector iter in the loop
attention: const_iterator

  1. for (std::vector<int>::const_iterator it = vector.begin(); it != vector.end(); it++)
  2. for(std::vector<int>::const_iterator i = (*it).begin();i != (*it).end();i++)
  3. cout << *i << ' ';

vector

vector.swap(vector_tmp);

In the beginning,I just wanna to clear the vector use vector.clear() ,after exploding on the web, I noticed that vector.swap(vector_temp) is a better operation that It not only clear the data but also allocate the memory.

a.insert(a.end(), b.begin(), b.end());
a.insert(std::end(a), std::begin(b), std::end(b));

Recently, I found a interesting method to return different data type with the help of struct.

  1. /*
  2. * define multi data struct
  3. * */
  4. struct infectedListInfo{
  5. vector<vector<Point> \*>\* infectedOutList;
  6. vector<Vec4i>* hierarchyList;
  7. int y_range[2];
  8. };

PS: Attention:
everytime you define the struct, It's nameSpace should be signed before the struct name.(became It's only use in your nameSpace, not global)
https://stackoverflow.com/questions/26415054/return-type-out-of-line-definition-differs-from-that-in-the-declaration

ref:
swap vector
http://www.cplusplus.com/reference/vector/vector/swap/
extend vector
https://stackoverflow.com/questions/313432/c-extend-a-vector-with-another-vector
how to implement a multi return.
http://blog.csdn.net/supreme42/article/details/7636475

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注