Unreasonable Input in std sort Lambda Expression

	struct Aa
	{
		int i0;
	};
	std::vector<Aa*> list_;
...
	std::sort(list_.begin(), list_.end(), [this, &end_dates](Aa* l, Aa* r) {
		return l->degree_ - r->degree_;
	});

Before the sort, I promise the members of list_ are all not null, but in the sort lambda expression, when running, we will be caught by a null pointer access SIGSEGV, for example, l is null.

Well, the good shot is,

...
	std::sort(list_.begin(), list_.end(), [this, &end_dates](Aa* l, Aa* r) {
		return l->degree_ > r->degree_;
	});

vector push_back后iterator还是否有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vector<int>::iterator it = vec.begin();
    it++;
    for (int i = 5; i < 10000; i++)
        vec.push_back(i);
    int j = *it; //这里报错
    return 1;
}

实验证明:vector push_back后原来的iterator就失效了。

1
2
3
4
5
6
7
8
    list<int> li;
    li.push_back(1);
    li.push_back(2);
    list<int>::iterator itList = li.begin();
    itList++;
    for (int i = 5; i < 10000; i++)
        li.push_back(i);
    int result = *itList;

实验证明:list push_back后原来的iterator依然有效。
如此说明,iterator中存放的是成员的内存地址,而非成员相对基地址的位移。